-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentScriptsLoader.js
191 lines (182 loc) · 5.6 KB
/
ContentScriptsLoader.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* ContentScriptsLoader
*
* Manages the insertion of content scripts to all matching tabs. This is useful
* so that after your extension is installed, it's available to be used in all
* available tabs right away. It essentially replicates a tab-reload without
* actually reloading it.
*
* It's important to note that you need the 'tabs' permission in order to query
* all the tabs and finding the appropriate ones for "content_scripts" blocks.
*
* All you need to do is call the insert method, and it will take care of
* iterating all tabs, and whichever ones match any of your "content_scripts"
* blocks (defined in your manifest), inserting the appropriate css and/or js.
*
* It will also take into consideration the "run_at" value, to ensure they're
* inserted as you designated in your manifest.
*
* @example
* chrome.runtime.onInstalled.addListener(
* function(details) {
* if (details.reason === 'install') {
* ContentScriptsLoader.insert().then(function() {
* alert('Extension code available in all tabs');
* });
* }
* }
* );
* @abstract
*/
window.ContentScriptsLoader = (function() {
/**
* __cloneArray
*
* @access private
* @param Array arr
* @return Array
*/
var __cloneArray = function(arr) {
return arr.slice(0);
};
/**
* __getManifest
*
* @access public
* @return Object
*/
var __getManifest = function() {
return chrome.runtime.getManifest();
};
/**
* __loadContentScripts
*
* @access private
* @return Promise
*/
var __loadContentScripts = function() {
return new Promise(function(resolve, reject) {
var manifest = __getManifest(),
contentScripts = manifest.content_scripts;
if (contentScripts.length === 0) {
resolve();
} else {
__loadContentScriptsRecursively(contentScripts).then(resolve);
}
});
};
/**
* __loadContentScriptsRecursively
*
* @access private
* @param Array contentScripts
* @return Promise
*/
var __loadContentScriptsRecursively = function(contentScripts) {
return new Promise(function(resolve, reject) {
if (contentScripts.length === 0) {
resolve();
} else {
var contentScript = contentScripts.shift(),
scripts = [],
runAt = contentScript.run_at;
for (var index in contentScript.css) {
scripts.push({
'type': 'css',
'value': contentScript.css[index]
});
}
for (index in contentScript.js) {
scripts.push({
'type': 'js',
'value': contentScript.js[index]
});
}
chrome.tabs.query(
{
url: contentScript.matches
},
function(tabs) {
__loadTabsRecursively(tabs, scripts, runAt).then(
function() {
__loadContentScriptsRecursively(contentScripts).then(
resolve
)
}
);
}
);
}
});
};
/**
* __loadScriptsRecursively
*
* @access private
* @param Array scripts
* @param Object tab
* @param String runAt
* @return Promise
*/
var __loadScriptsRecursively = function(scripts, tab, runAt) {
return new Promise(function(resolve, reject) {
if (scripts.length === 0) {
resolve();
} else {
if (tab.url.match(/^chrome\:\//i) === null) {
var script = scripts.shift(),
closure = chrome.tabs.executeScript;
if (script.type === 'css') {
closure = chrome.tabs.insertCSS;
}
closure(tab.id, {
file: script.value,
runAt: runAt
}, function() {
__loadScriptsRecursively(scripts, tab, runAt).then(
resolve
);
});
} else {
resolve();
}
}
});
};
/**
* __loadTabsRecursively
*
* @access private
* @param Array tabs
* @param Array scripts
* @param String runAt
* @return Promise
*/
var __loadTabsRecursively = function(tabs, scripts, runAt) {
return new Promise(function(resolve, reject) {
if (tabs.length === 0) {
resolve();
} else {
var tab = tabs.shift(),
clonedScripts = __cloneArray(scripts);
__loadScriptsRecursively(clonedScripts, tab, runAt).then(
function() {
__loadTabsRecursively(tabs, scripts, runAt).then(resolve);
}
);
}
});
};
// Public
return {
/**
* insert
*
* @access public
* @return Promise
*/
insert: function () {
return __loadContentScripts();
}
};
})();