如何在 Chrome 扩展内容脚本中使用 jQuery 而不发生冲突 [英] How to use jQuery in Chrome extensions contentscript without conflict

查看:30
本文介绍了如何在 Chrome 扩展内容脚本中使用 jQuery 而不发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不与网页上的脚本冲突的情况下在我的 google chrome 扩展中使用 jQuery?因为当我使用 jQuery 并且网页上的另一个脚本使用 $ 时,我的内容脚本会死亡或该网页会死亡,

how can I use jQuery in my google chrome extension without conflicting with the scripts on the webpage? because when I use jQuery and another script on the webpage uses $, my content script dies or that webpage dies,

推荐答案

真正的 答案是您不需要使用自运行私有函数".您需要了解内容脚本是独立执行,因此不能与使用的资源发生冲突由网站设计.

The real answer is that you don't need to use "self-running private functions". You need to understand that content scripts are executed in isolation so cannot conflict with resources used by websites by design.

如果您想在内容脚本中使用库,首选方法是将其简单地包含在您的扩展程序/应用程序中,然后首先将其加载到您的清单中;

If you want to use a library in your content script the preferred method is to simply include it in your extension/app and then load it first in your manifest;

{
  ...
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["jquery.js", "myscript.js"]
    }
  ]
  ...
}

这将导致 jquery.js 被加载到您的私有内容脚本环境中,然后是 myscript.js.您的代码将更加简洁和模块化,因为它不包含用于外部库的缩小代码.

This will result in jquery.js being loaded in to your private content script environment and then myscript.js. Your code will be much cleaner and modular as it doesn't contain minified code for external libraries.

来源: https://developer.chrome.com/extensions/content_scripts

这篇关于如何在 Chrome 扩展内容脚本中使用 jQuery 而不发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆