我发现的所有内容似乎都涉及注入(inject) jQuery,但我知道 jQuery 已经存在于页面上(浏览器 JS 控制台允许我使用它)。
我有一个后台脚本,它正在向正在运行该函数的内容脚本发送消息。当该函数执行时我得到:
Error in event handler for runtime.onMessage: ReferenceError: jQuery is not defined
(我尝试过 $ 和 jQuery 引用)
有什么想法为什么我无法从内容脚本访问现有的 jquery 对象吗?
最佳答案
这是因为内容脚本在设计上不允许访问页面定义的变量/函数
https://developer.chrome.com/extensions/content_scripts
However, content scripts have some limitations. They cannot:
- Use chrome.* APIs, with the exception of:
- extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
- i18n
- runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
- storage
- Use variables or functions defined by their extension's pages
- Use variables or functions defined by web pages or by other content scripts
因此,如果您想使用某个库,您的扩展必须注入(inject)它
关于javascript - 在 Chrome 扩展内容脚本中使用现有的 jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36957125/