fix: listeners out of limit warning (#12841)

When the Chrome Extension has too many content scripts (above default
10 counts), there will be a warning: possible EventEmitter memory leak
detected. 11 listeners added.
This commit is contained in:
biuuu 2018-05-08 13:10:11 +08:00 committed by Cheng Zhao
parent c13d1e8ae6
commit 86d023b02f

View file

@ -23,6 +23,12 @@ const runContentScript = function (extensionId, url, code) {
return compiledWrapper.call(this, context.chrome)
}
const runAllContentScript = function (scripts, extensionId) {
for (const {url, code} of scripts) {
runContentScript.call(window, extensionId, url, code)
}
}
const runStylesheet = function (url, code) {
const wrapper = `((code) => {
function init() {
@ -40,34 +46,36 @@ const runStylesheet = function (url, code) {
return compiledWrapper.call(this, code)
}
const runAllStylesheet = function (css) {
for (const {url, code} of css) {
runStylesheet.call(window, url, code)
}
}
// Run injected scripts.
// https://developer.chrome.com/extensions/content_scripts
const injectContentScript = function (extensionId, script) {
if (!script.matches.some(matchesPattern)) return
if (script.js) {
for (const {url, code} of script.js) {
const fire = runContentScript.bind(window, extensionId, url, code)
if (script.runAt === 'document_start') {
process.once('document-start', fire)
} else if (script.runAt === 'document_end') {
process.once('document-end', fire)
} else {
document.addEventListener('DOMContentLoaded', fire)
}
const fire = runAllContentScript.bind(window, script.js, extensionId)
if (script.runAt === 'document_start') {
process.once('document-start', fire)
} else if (script.runAt === 'document_end') {
process.once('document-end', fire)
} else {
document.addEventListener('DOMContentLoaded', fire)
}
}
if (script.css) {
for (const {url, code} of script.css) {
const fire = runStylesheet.bind(window, url, code)
if (script.runAt === 'document_start') {
process.once('document-start', fire)
} else if (script.runAt === 'document_end') {
process.once('document-end', fire)
} else {
document.addEventListener('DOMContentLoaded', fire)
}
const fire = runAllStylesheet.bind(window, script.css)
if (script.runAt === 'document_start') {
process.once('document-start', fire)
} else if (script.runAt === 'document_end') {
process.once('document-end', fire)
} else {
document.addEventListener('DOMContentLoaded', fire)
}
}
}