-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
68 lines (66 loc) · 2.13 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse){
if(request.query == 'getRequest'){
fetch(request.url)
.then(response => response.text())
.then(text => sendResponse(text))
.catch(error => alert(error));
return true;
}
else if(request.query == 'postRequest'){
console.log(JSON.stringify({
code: request.code,
stdin: request.stdin,
fileList: request.fileList
}));
fetch(request.url, {
method: "POST",
body: JSON.stringify({
code: request.code,
stdin: request.stdin,
fileList: request.fileList
})
})
.then(response => response.json)
.then(result => sendResponse(result))
.catch(error => console.log(error));
}
}
)
chrome.contextMenus.removeAll();
const Menu = chrome.contextMenus.create({
id: "Judge",
title: "EasyJudge ― Judge code",
contexts: ["link"]
});
chrome.storage.local.get(["judge"], function(items){
const judgeValue = items.judge;
const judgeJson = JSON.parse(judgeValue);
for(let index=1; index<=judgeJson.problemList.length; index++){
chrome.contextMenus.create({
title: 'Problem #' + index,
parentId: Menu,
id: index.toString(),
contexts: ["link"]
});
}
});
function genericOnClick(info){
const url = info.linkUrl;
const selText = info.selectionText;
if(url != undefined){
fetch(url)
.then(response => response.text())
.then(text => {
chrome.storage.local.set({"code" : text});
})
.catch(error => console.log(error));
}
else{
chrome.storage.local.set({"code" : selText});
}
const index = info.menuItemId;
chrome.storage.local.set({"popjudge": index});
chrome.storage.local.set({"runmode" : "judge"});
}
chrome.contextMenus.onClicked.addListener(genericOnClick);