-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
95 lines (85 loc) · 2.82 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var menuVariants = [30, 60, 120, 360, "custom", 0],
tabIds = [],
currentTabId,
timer,
menuItems = [];
chrome.tabs.onActivated.addListener(function(changeInfo) {
currentTabId = changeInfo.tabId;
chrome.tabs.get(currentTabId, function(tab){
if (tab.url.indexOf("opera://") == -1){
chrome.contextMenus.update('rootReloadMenuItem', {enabled: true});
if(!tabIds[currentTabId] || (tabIds[currentTabId] && !tabIds[currentTabId].timer)){
recreateMenu('reload_0');
}
else if(tabIds[currentTabId].timer) {
recreateMenu(tabIds[currentTabId].menuItemId);
}
}
else {
chrome.contextMenus.update('rootReloadMenuItem', {enabled: false});
}
});
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "resetMenu") {
recreateMenu('reload_0');
}
if (request.action == "startReload") {
if(request.value > 0){
startReloadPage(request.value, currentTabId, 'reload_custom');
}
}
});
function createMenu(activeMenuId){
chrome.contextMenus.create({
title: chrome.i18n.getMessage("rootMenuItem"),
id: "rootReloadMenuItem"
})
for (var i in menuVariants) {
chrome.contextMenus.create({
title: chrome.i18n.getMessage("menuItem"+menuVariants[i]),
parentId: "rootReloadMenuItem",
id: "reload_"+menuVariants[i],
type: "radio",
onclick: menuHandler,
checked: activeMenuId && activeMenuId == "reload_"+menuVariants[i]
});
};
};
createMenu("reload_0");
function customReload (data) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {run: "createBox"}, function(response) {});
});
}
function menuHandler(data){
if(data.menuItemId == "reload_custom"){
customReload(data);
}
else {
var time = parseInt(data.menuItemId.split("_")[1]);
time ? startReloadPage(time, currentTabId, data.menuItemId) : stopReloadPage(currentTabId);
}
}
function startReloadPage(interval, currentTabId, menuItemId){
if(tabIds[currentTabId] && tabIds[currentTabId]){
stopReloadPage(currentTabId);
}
tabIds[currentTabId] = {
'interval': interval,
'timer': setInterval(function(){
chrome.tabs.reload(currentTabId);
}, parseInt(interval)*1000),
'menuItemId': menuItemId
};
}
function stopReloadPage(currentTabId){
if(tabIds[currentTabId] && tabIds[currentTabId].timer){
clearInterval(tabIds[currentTabId].timer);
tabIds[currentTabId].timer = false;
}
}
function recreateMenu(menuId) {
chrome.contextMenus.removeAll();
createMenu(menuId);
}