javascript - 在 Chrome 扩展内容脚本中使用现有的 jQuery?

标签 javascript jquery google-chrome google-chrome-extension sendmessage

我发现的所有内容似乎都涉及注入(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/

相关文章:

javascript - 复选框内的图像

javascript - 密码中允许使用多个特殊字符

javascript - Sencha Touch 日期代码在 Chrome 中运行良好,但在 Safari 中失败

javascript - 超时-Dialog.js 问题

javascript - 使用JS改变样式

jquery - 主干/下划线 : How to get index of a model inside render?

jquery 数据表功能不适用于 asp :repeater

javascript - JQuery 如何创建它的自定义事件?我可以在 Javascript 中重新创建吗?

javascript - 在 javascript 中添加数字和数组会产生奇怪的结果

javascript - Chrome 忽略动态更改元素的相对定位