Chrome 扩展的内容脚本不会影响特定网站
Chrome extension's content scripts doesn't affect a particular website
我有一个 chrome 扩展程序,它使用内容脚本将特定词插入网页。它在某个网站 x 中有效,但在 y 中无效。
清单
{
"name": "test",
"version": "0.0.1",
"manifest_version": 2,
"description": "word",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_title": "chat"
},
"permissions": [
"https://*/*",
"http://*/*",
"tabs"
]
}
Background.js
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
Inject.js
// this is the code which will be injected into a given page...
(function() {
// just place a div at top right
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.top = 600;
div.style.right = 700;
div.textContent = 'Hello!';
document.body.appendChild(div);
})();
此代码适用于在网站中添加单词:https://developer.chrome.com/extensions/examples/api/browserAction/make_page_red/background.js
在 web.whatsapp.com 以及其他常见网站中不起作用。
刚刚将 "*://*/*",
添加到您的清单中以使其正常工作。
manifest.json
{
"name": "test",
"version": "0.0.1",
"manifest_version": 2,
"description": "word",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_title": "chat"
},
"permissions": [
"*://*/*",
"tabs"
]
}
background.js
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
inject.js
(function() {
console.log("Inject successfully.");
// just place a div at top right
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.top = 600;
div.style.right = 700;
div.style.zIndex = 9999;
div.textContent = 'Hello!';
document.body.appendChild(div);
})();
我有一个 chrome 扩展程序,它使用内容脚本将特定词插入网页。它在某个网站 x 中有效,但在 y 中无效。
清单
{
"name": "test",
"version": "0.0.1",
"manifest_version": 2,
"description": "word",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_title": "chat"
},
"permissions": [
"https://*/*",
"http://*/*",
"tabs"
]
}
Background.js
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
Inject.js
// this is the code which will be injected into a given page...
(function() {
// just place a div at top right
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.top = 600;
div.style.right = 700;
div.textContent = 'Hello!';
document.body.appendChild(div);
})();
此代码适用于在网站中添加单词:https://developer.chrome.com/extensions/examples/api/browserAction/make_page_red/background.js
在 web.whatsapp.com 以及其他常见网站中不起作用。
刚刚将 "*://*/*",
添加到您的清单中以使其正常工作。
manifest.json
{
"name": "test",
"version": "0.0.1",
"manifest_version": 2,
"description": "word",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_title": "chat"
},
"permissions": [
"*://*/*",
"tabs"
]
}
background.js
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
inject.js
(function() {
console.log("Inject successfully.");
// just place a div at top right
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.top = 600;
div.style.right = 700;
div.style.zIndex = 9999;
div.textContent = 'Hello!';
document.body.appendChild(div);
})();