admin管理员组文章数量:1295338
I have an chrome extension, with 2 content script injected by manifest and one background script.
{
"manifest_version": 2,
"name": "Test",
"permissions": [
"tabs", "<all_urls>", "activeTab", "storage"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": [
"content/autofill/lib_generic.js",
"content/autofill/lib.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": [
"content/specific_scripts/*"
],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
lib_generic.js
contains one function named apply_forms(...)
(its description is not important). The function is called from lib.js
file. But this procedure doesn't work with several pages, so for each such page a I have a special script - also with only one function named apply_forms(...)
.
I have a function, which takes current domain as input and returns name of desired specific script or false
if generic should be used.
There is too many files and it's logic is more plicated, so I can't just list all (url, script)
pairs in "content_scripts"
directive (I also don't want to inject all specific files as content script).
I've tried something like this in background (note that it's only for demonstration):
var url = ""; //url of current tab
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "plete") {
var filename = getSpecificFilename(url);
chrome.tabs.executeScript(tabId, {file: filename}, function() {
//script injected
});
}
});
NOTE: getSpecificFilename(...) will always return a name
But I get Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL
on the 5th line.
Can anyone help me with this? Is it the good way to "override` function definition dynamically, or should I go different way (which one, then).
Thanks.
I have an chrome extension, with 2 content script injected by manifest and one background script.
{
"manifest_version": 2,
"name": "Test",
"permissions": [
"tabs", "<all_urls>", "activeTab", "storage"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": [
"content/autofill/lib_generic.js",
"content/autofill/lib.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": [
"content/specific_scripts/*"
],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
lib_generic.js
contains one function named apply_forms(...)
(its description is not important). The function is called from lib.js
file. But this procedure doesn't work with several pages, so for each such page a I have a special script - also with only one function named apply_forms(...)
.
I have a function, which takes current domain as input and returns name of desired specific script or false
if generic should be used.
There is too many files and it's logic is more plicated, so I can't just list all (url, script)
pairs in "content_scripts"
directive (I also don't want to inject all specific files as content script).
I've tried something like this in background (note that it's only for demonstration):
var url = ""; //url of current tab
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "plete") {
var filename = getSpecificFilename(url);
chrome.tabs.executeScript(tabId, {file: filename}, function() {
//script injected
});
}
});
NOTE: getSpecificFilename(...) will always return a name
But I get Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL
on the 5th line.
Can anyone help me with this? Is it the good way to "override` function definition dynamically, or should I go different way (which one, then).
Thanks.
Share Improve this question asked Sep 8, 2014 at 14:05 dakovdakov 1,1393 gold badges13 silver badges34 bronze badges 1- "Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL" => stackoverflow./questions/19042857/… – kol Commented Sep 8, 2014 at 14:11
2 Answers
Reset to default 6This means, probably, that you're getting an onUpdated
event on an extension/internals page (popup? options page? detached dev tools?).
One option is to filter by URL:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "plete") {
if(!tab.url.match(/^http/)) { return; } // Wrong scheme
var filename = getSpecificFilename(url);
chrome.tabs.executeScript(tabId, {file: filename}, function() {
//script injected
});
}
});
Another (and probably better) option is to make your content script request this injection:
// content script
chrome.runtime.sendMessage({injectSpecific : true}, function(response) {
// Script injected, we can proceed
if(response.done) { apply_forms(/*...*/); }
else { /* error handling */ }
});
// background script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if(message.injectSpecific){
var filename = getSpecificFilename(sender.url);
chrome.tabs.executeScript(sender.tab.id, {file: filename}, function() {
sendResponse({ done: true });
});
return true; // Required for async sendResponse()
}
});
This way you know that a content script is injected and initiated this.
You already have content scripts on every page, you can use them to load additional scripts with import(chrome.runtime.getURL('other.js))
.
If you use webpack or Parcel, this is even easier to handle because they also create that other.js
file:
- https://github./awesome-webextension/webpack-target-webextension
- https://parceljs/recipes/web-extension/
本文标签: javascriptDynamic loading of content script (chrome extension)Stack Overflow
版权声明:本文标题:javascript - Dynamic loading of content script (chrome extension) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741617012a2388582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论