- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有这个简单的扩展,它在 chrome 的工具栏上显示图标并显示启用/禁用按钮。我想向按钮添加功能以禁用或启用在访问谷歌网站时触发的 content_script.js
:
弹出窗口
var setLayout = function(){
var list = document.createElement('ul');
var enable = document.createElement('li');
enable.appendChild(document.createTextNode('Enable Script'));
enable.onclick = function(){toggle(0)};
var disable = document.createElement('li');
disable.appendChild(document.createTextNode('Disable Script'));
disable.onclick = function(){toggle(1)};
list.appendChild(disable);
document.body.appendChild(list);
function toggle(n){
list.removeChild( n == 0 ? enable : disable);
list.appendChild(n == 0 ? disable : enable);
}
};
document.addEventListener('DOMContentLoaded', setLayout, false);
list .json
{
"manifest_version": 2,
"name": "test",
"description": "test",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["https://www.google.co.uk/"],
"js": ["content_scripts.js"]
}
]
}
content_scripts.js
(function(){
alert('hello');
})();
我是 google 扩展的新手,不知道该怎么做,我想在单击禁用/启用按钮后更改 manifist,但在阅读 google 网站上的文档后找不到正确的命令来执行此操作!
如有任何帮助,我们将不胜感激。
最佳答案
经过一番研究,我想出了如何使用backround pages
、sendMessage
和localstorage
来解决这个问题。
background pages
充当 popup.js
和 content_scripts.js
之间的通信器,它们位于两个不同的文档中,并且不可能直接在它们之间传递变量。
要在 mamifest 中启用背景页面,我添加了:
"background": {
"scripts": ["background.js"],
"persistent": true
},
localstorage
在本地保存变量并记住它们,即使浏览器关闭并再次打开,所以当启用/禁用按钮被点击时它设置 localStorage['status'] = 1/0
可以通过 background.js
访问并传递给 content_scripts.js
。
要设置我添加到 popup.js
的 localStorage 变量:
if(!localStorage.status) localStorage['status'] = 1;
toggle(2);
enable.onclick = function(){toggle(0)};
disable.onclick = function(){toggle(1)};
function toggle(n){
if((n == 0) && (enable.parentNode == list)){
list.removeChild(enable);
list.appendChild(disable);
localStorage.status = 1;
}else if((n == 1) && (disable.parentNode == list)){
list.removeChild(disable);
list.appendChild(enable);
localStorage.status = 0;
}else if((n == 2) && (!list.hasChildNodes())){
list.appendChild((localStorage.status == 1) ? disable : enable);
chrome.browserAction.setIcon({path: (localStorage.status == 1) ? "icons/icon19.png" : "icons/icon19_disabled.png"});
}else{
return;
}
}
要将 localStorage.status
传递给 content_scripts.js
我必须在 content_scrips.js
上使用 sendMessage
加载时向 background.js
发送请求消息,background.js
上的 onMessage
监听请求并向 content_scripts 发送响应。 js
与 localStorage.status
值。
背景.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == "status") sendResponse({status: localStorage.status});
});
content_scripts.js
var fn = function(){...};
chrome.runtime.sendMessage({type: "status"}, function(response) {
if(response.status == 1) fn();
return;
});
就是这样,希望有人觉得它有用。
关于javascript - 我如何在谷歌扩展中打开/关闭 content_scripts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17174093/
这不是重复,我已经检查过问题 Chrome extension: Modifying the content of a webpage 但它没有回答我的问题,因为我只想在特定时间阻塞,因此阻塞/不阻塞
我正在开发一个扩展程序,希望使其与我的网站进行交互。但是当我尝试调用位于“content_script.js”文件中的函数时,它说该函数未定义! 最佳答案 第 1 步:了解 isolated cont
我正在尝试制作一个 Chrome 扩展程序,它将在我的 Gmail 中显示一个弹出窗口,其中包含来 self 的外部服务器的一些信息,例如今天的天气。 我尝试过的 我尝试将 js 包含在 chrome
我有这个简单的扩展,它在 chrome 的工具栏上显示图标并显示启用/禁用按钮。我想向按钮添加功能以禁用或启用在访问谷歌网站时触发的 content_script.js: 弹出窗口 var setLa
我有一个简单的 manifest.json,但出于某种原因,我一直遇到这个错误 无法为内容脚本加载 JavaScript“content_script.js” 一旦我添加 content_script
我对制作 Chrome 扩展程序很陌生,我会尝试一些东西: 我将把 JS 变量从“browser_action”发送到“content_scripts” "browser_action": {
我正在开发 Chrome 扩展,我需要将 javascript 文件加载到内容脚本,但该文件是通过 webpack-dev-server 提供的。所以它只能在本地主机上使用。 我尝试更改我的 mani
最初在这里发布这个问题: https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/wbSpXvnO10A/nov36skm
我刚刚开始使用 JavaScript,正在尝试修改 MDN 教程中的脚本,Your First WebExtension 我正在尝试在网页周围绘制红色或蓝色框,具体取决于它是 http://还是 ht
我认为这是一个经常被问到的问题,但我找不到答案。有没有办法让 Chrome 扩展程序使其浏览器操作弹出窗口仅在 content_scripts 匹配页面上可用? 例如:我为一个网站做了一个扩展。我希望
最近,我制作了一个 Chrome 扩展来强制所有网页使用给定的字体。它曾经工作得很好,但从昨天(也许更早)开始就停止工作了。 扩展的关键代码是: manifest.json file content:
我正在编写一个包含 content_script 的小型 Chrome 扩展程序。 它将在单个域上运行,我正在尝试稍微改进一个站点。 我想在我的内容脚本中使用 jQuery,但是网站也使用 jQuer
我有一个测试 Chrome 扩展程序,运行于 。它运行一个仅写入域的 content_script。 它在所有情况下都工作正常,除非我编写不存在的域。 扩展的目的是在特定域上运行,无论发生什么情况。
我正在开发一个供个人使用的扩展程序,它只会自动重复网站 A 上的某些操作。这基本上是我想使用 Chrome 扩展程序自动化的流程: 触发按钮点击事件。 打开一个新的弹窗(由网站A打开)。 在打开的窗口
我想将一个已保存的变量从内容脚本传递到弹出页面,这样我就可以简单地输出它。例如,如果我的内容脚本具有以下代码: var x = 'abc'; 我希望弹出窗口标题如此可变。 最佳答案 有几种方法可以做到
我是 javascript 的新手,正在尝试学习 Chrome 扩展程序。我搜索了我能想到的问题的所有变体,但我找到的答案不适用于我的特定问题。我已经阅读了 chrome 扩展的教程,但我认为应该有效
如何更改在 content_scripts (manifest.json) 中声明的 CSS 文件? 我需要将它从 background.js 更改(style.css 到 style2.css) 有
我正在尝试学习如何使用 content_script.js 将请求发送到我的 chrome 扩展程序的后台页面。我读过很多关于此的答案,但没有一个对我有帮助。我似乎无法让监听器执行。 Manifest
到目前为止,我还没有在我的 Firefox 中安装任何插件,也没有使用任何额外的 JS 脚本,但我没有从启用此 content_script.js 的地方打印不必要的消息像这样的控制台: conte
我正在尝试从外部服务器加载 JS,以便可以更新和开发我的扩展程序,而无需更新 chrome 扩展程序。这是我目前的 list : { "name": "Test", "version": "1
我是一名优秀的程序员,十分优秀!