-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
78 lines (69 loc) · 2.24 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
/*global chrome, RegExp */
(function (chrome, RegExp) {
'use strict';
var replacements,
updateReplacements = function (complete) {
chrome.storage.sync.get({
replacements: ''
}, function (items) {
var serializedReplacements = items.replacements.split('\n'),
serializedReplacement,
i;
replacements = [];
for (i = 0; i < serializedReplacements.length; i += 1) {
// http://upshots.org/javascript/javascript-regexp-to-remove-comments
serializedReplacement = serializedReplacements[i].replace(/(\/\*([\s\S]*?)\*\/)|(\/\/([\w\W]*)$)/gm, '');
if (serializedReplacement) {
serializedReplacement = serializedReplacement.split('-->');
replacements.push([
new RegExp(serializedReplacement[0]),
serializedReplacement[1]
]);
}
}
if (complete !== undefined) {
complete();
}
});
},
registerTabListener = function () {
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === 'complete') {
var originalTitle = tab.title,
newTitle,
replacement,
replaced = false,
i;
for (i = 0; i < replacements.length; i += 1) {
replacement = replacements[i];
if ((newTitle = originalTitle.replace(replacement[0],
replacement[1])) !== originalTitle) {
replaced = true;
break;
}
}
if (replaced) {
chrome.tabs.sendMessage(tabId, {
title: newTitle
});
}
chrome.runtime.sendMessage({
replaced: {
originalTitle: originalTitle,
title: newTitle,
rule: (replaced ? [
replacement[0].source,
replacement[1]
] : null)
}
});
}
});
};
updateReplacements(registerTabListener);
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.updated === 'replacements') {
updateReplacements();
}
});
}(chrome, RegExp));