-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackground.js
66 lines (52 loc) · 1.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
var separator = "] "
var total = 0
var enabled = true
function indexTitle(index, title, max) {
var start = title.indexOf(separator) > -1 ? (title.indexOf(separator) + separator.length) : -1
var originalTitle = title.substring(start)
if (enabled == false) { return originalTitle }
if (index > 7 && index == max-1) {
return "".concat("9".toString(), separator, originalTitle)
}
else if (index > 7) {
if(title[0] === "9" && title[1] === "]"){
return title.substring(2);
} else{
return title;
}
}
else {
return "".concat((index+1).toString(), separator, originalTitle)
}
}
function tabs() {
chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, function(tabs) {
total = tabs.length
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.executeScript(tabs[i].id,
{ code: "chrome.runtime.sendMessage({'index': ".concat(tabs[i].index,
", 'id': ",
tabs[i].id,
", 'title': '",
tabs[i].title,
"' })") })
}
}
)}
function callbackTab(tab){
if(enabled){
enabled = true;
tabs()
}
}
chrome.commands.onCommand.addListener(function(command) {
enabled = enabled ? false : true;
tabs()
})
chrome.tabs.onUpdated.addListener((tab) => callbackTab(tab));
chrome.tabs.onActivated.addListener((tab) => callbackTab(tab));
chrome.tabs.onRemoved.addListener((tab) => callbackTab(tab));
chrome.tabs.onMoved.addListener((tab) => callbackTab(tab));
chrome.runtime.onMessage.addListener(function(message, sender) {
chrome.tabs.executeScript(message.id, { code: "document.title=".concat('"', indexTitle(message.index, message.title, total), '"') } )
})