问题
Contentscripts don't seem to work for Worker, SharedWorker, ServiceWorker, and other nonpages.
Is there a simple way to inject a script into the start of them?
Sample contentscript runat docstart:
var script = document.createElement("script")
script.textContent = `
console.log = function() { return 0 }
alert(console.log) // overridden, good
var blob = new Blob([
'console.log(Math.random())' // not overriden, bad
], { type: "text/javascript" })
var worker = new Worker(window.URL.createObjectURL(blob));
`
document.documentElement.appendChild(script)
Sure, it's possible to inject a script into the document itself and rewrite the constructors of Worker etc to include the script, but there had to be a simpler solution.
Any ideas?
来源:https://stackoverflow.com/questions/46781770/chrome-extension-script-injection-to-workers-and-other-nonpages