简体   繁体   English

如何动态添加或删除Chrome扩展程序,内容脚本,javascript文件?

[英]How do I add or remove Chrome extension, content-script, javascript files dynamically?

I'm making a chrome extension. 我正在制作镀铬扩展程序。 I want to add or remove a few content script files from a popup page, using javascript, when the extension is running. 我想在扩展程序运行时使用javascript从弹出页面添加或删除一些内容脚本文件。

In my extension, the user picks a color from popup page. 在我的扩展程序中,用户从弹出页面中选择一种颜色。 There are 5 colors. 有5种颜色。 So I have 5 javascript files -- one for each color. 所以我有5个javascript文件 - 每种颜色一个。

So, what javascript code, in the popup page, will change the running content script javascript file? 那么,弹出页面中的哪些javascript代码会改变运行内容脚本javascript文件?

First off, if you have 5 JS files, one for each of 5 colors, this smacks of "Clone And Modify Programming" -- which is a bad habit to be in. 首先,如果你有5个JS文件,每个5个颜色一个,这就是“克隆和修改编程” - 这是一个坏习惯。

The smart thing to do is to have one JS file, with "DRY" code that switches off color as a parameter. 聪明的做法是拥有一个 JS文件,其中“DRY”代码将颜色作为参数关闭。 See "Dont Repeat Yourself" (sic). 请参阅“不要重复自己” (原文如此)。


To load whole content scripts, from your extension's background, see "Programmatic injection" . 要加载整个内容脚本,请从扩展程序的背景中查看“程序化注入”



As for adding or removing javascript files dynamically... 至于动态添加或删除javascript文件...

  1. You can't remove a file once it is loaded. 加载后无法删除文件。 Even if it is in a <script> node and you delete that, the parsed functions, variables, timers, event-listeners, etc. will still be active in memory. 即使它在<script>节点中并且您删除它,解析的函数,变量,计时器,事件监听器等仍将在内存中处于活动状态。

    1. You can overwrite named objects. 您可以覆盖命名对象。 For example, if File‗1.js globally sets x=7; 例如,如果File‗1.js全局设置x=7; , you can later load File‗2.js to set x=11; ,你可以稍后加载File‗2.js来设置x=11; .
    2. You can cancel named timers -- but you must explicitly code each one. 您可以取消已命名的计时器 - 但必须对每个计时器进行显式编码。
    3. You can cancel known event listeners, but you must again deliberately code this. 您可以取消已知的事件侦听器,但必须再次故意对此进行编码。
    4. You can't override anonymous objects ( (function () {... ...} )() ). 不能覆盖匿名对象( (function () {... ...} )() )。
    5. You can't cancel anonymous timers. 无法取消匿名计时器。


    In most cases, this is all more trouble than it is worth. 在大多数情况下,这比它的价值更麻烦。 Best to design your code to where it's not necessary. 最好将代码设计到不必要的地方。

  2. You can add JS (or other kinds of files) dynamically, see below. 您可以动态添加JS(或其他类型的文件),请参阅下文。


To add JS files dynamically... 要动态添加JS文件......

  1. Make sure that the file(s) are listed in the web_accessible_resources section of your manifest.json file. 确保manifest.json文件的web_accessible_resources部分中列出了文件。 For example: 例如:

     { "manifest_version": 2, "content_scripts": [ { "exclude_globs": [ ], "include_globs": [ ], "js": [ "MyContentScript.js" ], "matches": [ "http://YOUR_SERVER.COM/YOUR_PATH/*" ] } ], "name": "Dynamically load JS, demo", "version": "1", "web_accessible_resources": [ "MyJavascript_1.js", "MyJavascript_2.js", "MyJavascript_3.js" ] } 


  2. Then, in your content script ( MyContentScript.js ), you can use chrome.extension.getURL , like so: 然后,在您的内容脚本( MyContentScript.js )中,您可以使用chrome.extension.getURL ,如下所示:

     var D = document; var scriptNode = D.createElement ('script'); scriptNode.type = "text/javascript"; scriptNode.src = chrome.extension.getURL ("MyJavascript_2.js"); var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement; targ.appendChild (scriptNode); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Chrome扩展程序:如何在全局范围内捕获/处理内容脚本错误? - Chrome extension: how to trap/handle content-script errors globally? 如何为 URL 中的给定哈希触发 Chrome 扩展程序、内容脚本? - How to trigger a Chrome extension, content-script for a given hash in the URL? 开发Google Chrome扩展程序时,如何在不同的时间包括内容脚本文件? - How to include content-script files at different moments when developing a google chrome extension? 从chrome扩展的内容脚本动态注入jQuery后,为什么不能使用jQuery? - Why can't I use jQuery after it's dynamically injected from a chrome extension's content-script? Chrome 扩展内容脚本 javascript - run_at document_end 无法正常工作 - Chrome extension content-script javascript - run_at document_end not working correctly 使用Chrome扩展程序内容脚本从localStorage中获取令牌值 - Getting a token value out of localStorage with a Chrome extension content-script 在Chrome扩展程序内容脚本中切换div - Toggling div inside chrome extension content-script Chrome 扩展程序:无法获取向内容脚本发送消息的背景 - Chrome Extension: Cannot get background to sendMessage to content-script 将选项页面中的设置交换为Chrome扩展程序中的内容脚本 - Exchanging Settings from Options Page to Content-Script in Chrome Extension 我们如何通过“ on-event”属性在Chrome内容脚本中调用函数 - How do we call a function inside the Chrome content-script from “on-event” attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM