Chrome extension: script injection to workers and other nonpages

夙愿已清 提交于 2021-02-10 17:30:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!