Fix: cleanup injectContent
This commit is contained in:
parent
8207d608be
commit
33200dbde4
|
@ -324,46 +324,21 @@ function onRuntimeMessage(request, sender, sendResponse) {
|
||||||
return KEEP_CHANNEL_OPEN;
|
return KEEP_CHANNEL_OPEN;
|
||||||
|
|
||||||
case 'injectContent':
|
case 'injectContent':
|
||||||
injectContent(request, sender.tab.id).then(sendResponse);
|
injectContent(sender.tab.id, request).then(sendResponse);
|
||||||
return KEEP_CHANNEL_OPEN;
|
return KEEP_CHANNEL_OPEN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function injectContent({resources}, tabId) {
|
function injectContent(tabId, {files}) {
|
||||||
return Promise.all(doInject())
|
return Promise.all(files.map(inject))
|
||||||
.then(() => ({status: 'success'}))
|
.then(() => ({status: 'success'}))
|
||||||
.catch(err => ({status: 'error', error: err.message}));
|
.catch(err => ({status: 'error', error: err.message}));
|
||||||
|
|
||||||
function *doInject() {
|
function inject(file) {
|
||||||
for (const resource of resources) {
|
|
||||||
const type = resource.match(/\.\w+$/i)[0];
|
|
||||||
if (type === '.js') {
|
|
||||||
yield injectScript(resource, tabId);
|
|
||||||
} else if (type === '.css') {
|
|
||||||
yield injectStyle(resource, tabId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function injectScript(url, tabId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
chrome.tabs.executeScript(tabId, {
|
const method = file.endsWith('.js') ? 'executeScript' : 'insertCSS';
|
||||||
file: url,
|
chrome.tabs[method](tabId, {
|
||||||
runAt: 'document_start'
|
file,
|
||||||
}, () => {
|
|
||||||
if (chrome.runtime.lastError) {
|
|
||||||
reject(chrome.runtime.lastError);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function injectStyle(url, tabId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
chrome.tabs.insertCSS(tabId, {
|
|
||||||
file: url,
|
|
||||||
runAt: 'document_start'
|
runAt: 'document_start'
|
||||||
}, () => {
|
}, () => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
|
|
|
@ -232,7 +232,7 @@ function createSourceLoader() {
|
||||||
function initUsercssInstall() {
|
function initUsercssInstall() {
|
||||||
pendingResource = communicate({
|
pendingResource = communicate({
|
||||||
method: 'injectContent',
|
method: 'injectContent',
|
||||||
resources: [
|
files: [
|
||||||
'/js/dom.js',
|
'/js/dom.js',
|
||||||
'/js/localization.js',
|
'/js/localization.js',
|
||||||
'/js/usercss.js',
|
'/js/usercss.js',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user