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:
parent
c13d1e8ae6
commit
86d023b02f
1 changed files with 26 additions and 18 deletions
|
@ -23,6 +23,12 @@ const runContentScript = function (extensionId, url, code) {
|
||||||
return compiledWrapper.call(this, context.chrome)
|
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 runStylesheet = function (url, code) {
|
||||||
const wrapper = `((code) => {
|
const wrapper = `((code) => {
|
||||||
function init() {
|
function init() {
|
||||||
|
@ -40,14 +46,19 @@ const runStylesheet = function (url, code) {
|
||||||
return compiledWrapper.call(this, code)
|
return compiledWrapper.call(this, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const runAllStylesheet = function (css) {
|
||||||
|
for (const {url, code} of css) {
|
||||||
|
runStylesheet.call(window, url, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run injected scripts.
|
// Run injected scripts.
|
||||||
// https://developer.chrome.com/extensions/content_scripts
|
// https://developer.chrome.com/extensions/content_scripts
|
||||||
const injectContentScript = function (extensionId, script) {
|
const injectContentScript = function (extensionId, script) {
|
||||||
if (!script.matches.some(matchesPattern)) return
|
if (!script.matches.some(matchesPattern)) return
|
||||||
|
|
||||||
if (script.js) {
|
if (script.js) {
|
||||||
for (const {url, code} of script.js) {
|
const fire = runAllContentScript.bind(window, script.js, extensionId)
|
||||||
const fire = runContentScript.bind(window, extensionId, url, code)
|
|
||||||
if (script.runAt === 'document_start') {
|
if (script.runAt === 'document_start') {
|
||||||
process.once('document-start', fire)
|
process.once('document-start', fire)
|
||||||
} else if (script.runAt === 'document_end') {
|
} else if (script.runAt === 'document_end') {
|
||||||
|
@ -56,11 +67,9 @@ const injectContentScript = function (extensionId, script) {
|
||||||
document.addEventListener('DOMContentLoaded', fire)
|
document.addEventListener('DOMContentLoaded', fire)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (script.css) {
|
if (script.css) {
|
||||||
for (const {url, code} of script.css) {
|
const fire = runAllStylesheet.bind(window, script.css)
|
||||||
const fire = runStylesheet.bind(window, url, code)
|
|
||||||
if (script.runAt === 'document_start') {
|
if (script.runAt === 'document_start') {
|
||||||
process.once('document-start', fire)
|
process.once('document-start', fire)
|
||||||
} else if (script.runAt === 'document_end') {
|
} else if (script.runAt === 'document_end') {
|
||||||
|
@ -69,7 +78,6 @@ const injectContentScript = function (extensionId, script) {
|
||||||
document.addEventListener('DOMContentLoaded', fire)
|
document.addEventListener('DOMContentLoaded', fire)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the request of chrome.tabs.executeJavaScript.
|
// Handle the request of chrome.tabs.executeJavaScript.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue