-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
88 lines (81 loc) · 2.73 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
var onClickListener = function (callback) {
// Max time between click events occurrence;
var CONTROL_TIME = 500;
var click = 0;
var timer;
if (callback && callback instanceof Function) {
return function (tab) {
click += 1;
clearTimeout(timer);
timer = setTimeout(function () {
//Clear all timers
clearTimeout(timer);
callback.apply(this ,[tab, click]);
click = 0;
}, CONTROL_TIME);
};
}
throw new Error('[InvalidArgumentException]');
};
chrome.browserAction.onClicked.addListener(
new onClickListener(
function onclick(tab, click) {
changeUrl(tab, click);
},
)
);
function changeUrl (tab, click) {
var url = '';
var el = document.createElement('a');
el.href = tab.url;
if (click === 3) {
chrome.browserAction.setIcon({'path': 'web.png'});
if (el.pathname.indexOf('/web') === -1) {
url = el.origin + '/web?tc';
}
else {
url = el.origin + '/?tc';
}
}
else {
if (el.search.indexOf('?debug') !== -1) {
if (click==2 && el.search.indexOf('?debug=assets') === -1) {
url = el.origin + el.pathname + '?debug=assets' + el.hash;
chrome.browserAction.setIcon({'path': 'super_on.png'});
} else {
url = el.origin + el.pathname + el.hash;
chrome.browserAction.setIcon({'path': 'off.png'});
}
} else {
if (click==1) {
url = el.origin + el.pathname + '?debug' + el.hash;
chrome.browserAction.setIcon({'path': 'on.png'});
} else {
url = el.origin + el.pathname + '?debug=assets' + el.hash;
chrome.browserAction.setIcon({'path': 'super_on.png'});
}
}
}
chrome.tabs.update(tab.id, {'url': url});
}
function changeIcon () {
chrome.tabs.query({active: true, currentWindow: true}, function (tab) {
var icon = 'off.png';
var el = document.createElement('a');
el.href = tab[0].url;
if (el.search.indexOf('?debug') !== -1) {
if (el.search.indexOf('?debug=assets') !== -1) {
icon = 'super_on.png';
} else {
icon = 'on.png';
}
}
if (el.search.indexOf('?tc') !== -1) {
icon = 'web.png';
}
chrome.browserAction.setIcon({'path': icon});
});
}
chrome.tabs.onActivated.addListener(function () { changeIcon(); });
chrome.tabs.onUpdated.addListener(function () { changeIcon(); });
chrome.windows.onFocusChanged.addListener(function () { changeIcon(); });