本文介绍了Chrome扩展程序:加载并执行外部脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法在我的Chrome扩展中加载和执行外部js脚本。看起来与相同,但我仍然无法确定为什么它不起作用在我的情况。
这个想法是,我想在我的内容脚本中有一些默认的功能,它应该解析一个网页内容。对于一些特定的网页,我想加载和使用特定的解析器,所以我尝试为wep页面加载适当的js脚本,并且此脚本应该扩展默认解析器的功能。
$现在我尝试只执行外部脚本的代码,但有这样的错误:在运行tabs.executeScript时未经检查的runtime.lastError:没有在Object.callback中指定的源代码或文件
这是我的 manifest.json :
<$ p $ {
name:Extension name,
version:1.2,
description:我的Chrome扩展名,
browser_action:{
default_popup:popup.html,
},
content_scripts:[{
css:[
style.css
],
js:[
bower_components / jquery / dist / jquery.js,
bower_components / bootstrap / dist / js / bootstrap .js,
content.js
,
matches:[*:// * / *]
}],
web_accessible_resources:[
frame.html,
logo-48.png
],
图标:{
16:logo-16.png,
48:logo-48.png,
128:logo-128.png
},
权限:[
标签,
storage,
http:// * /,
https:// * /
,
manifest_version:2
}
这是 popup.html
<!doctype html>
< html>
< head>
< title>标题< / title>
< script src =popup.js>< / script>
< / head>
< body>
< ul>
< li>一些链接< / li>
< / ul>
< / body>
< / html>
在 popup.js 中,我执行如下脚本:
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
chrome.tabs。 executeScript(tabs [0] .id,{file:http://127.0.0.1:8000/static/plugin/somesite.js});
});
我错了什么,我错过了什么?或者我应该使用另一种方法来解决问题?解析方案
将阻止甚至不发布您的扩展。所有脚本都必须位于扩展名中。但是有一个解决方案,来自google chrome文档的
的一部分代码:
var xhr = new XMLHttpRequest();
xhr.open(GET,http://127.0.0.1:8000/static/plugin/somesite.js,true);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
//警告!可能正在评估一个邪恶的脚本!
var resp = eval((+ xhr.responseText +));
//或者如果它工作的话
chrome.tabs.executeScript(tabs [0] .id,{code:xhr.responseText});
}
}
xhr.send();
或者您可以使用一些库,或。
如果您在代码中添加eval,您必须在manifest.json中添加以下代码:
content_security_policy:script -src'self''unsafe-eval'; object-src'self'`,
I have trouble loading and executing external js-script into my chrome extension. Looks the same as this question, but I still cant't figure out why it doesn't work in my case.
The idea is that I want to have in my content script some default function which should parse a web-page content. And for some specific web-pages I want to load and use specific parsers, so I try to load proper js-script for a wep-page, and this script shoud extend functionality of default parser.
By now I try only execute code from external script, but have such error: Unchecked runtime.lastError while running tabs.executeScript: No source code or file specified at Object.callback
This is my manifest.json:
{
"name": "Extension name",
"version": "1.2",
"description": "My chrome extension",
"browser_action": {
"default_popup": "popup.html",
},
"content_scripts": [{
"css": [
"style.css"
],
"js": [
"bower_components/jquery/dist/jquery.js",
"bower_components/bootstrap/dist/js/bootstrap.js",
"content.js"
],
"matches": ["*://*/*"]
}],
"web_accessible_resources": [
"frame.html",
"logo-48.png"
],
"icons": {
"16": "logo-16.png",
"48": "logo-48.png",
"128": "logo-128.png"
},
"permissions": [
"tabs",
"storage",
"http://*/",
"https://*/"
],
"manifest_version": 2
}
This is popup.html
<!doctype html>
<html>
<head>
<title>Title</title>
<script src="popup.js"></script>
</head>
<body>
<ul>
<li>Some link</li>
</ul>
</body>
</html>
And in popup.js i execute scrip like this:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {file: "http://127.0.0.1:8000/static/plugin/somesite.js"});
});
What am I dong wrong, did I miss something? Or should I use another approach to solve the issue?
解决方案
Running scripts from external sources like you try is forbidden by google chrome and will block or even not publish your extension. All scripts must be in the extension. But there is a solution,from google chrome doc:
If you need badly an external source, you can do a XML HTTP request and use the eval to the content. Here is a part of code from google doc:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://127.0.0.1:8000/static/plugin/somesite.js", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// WARNING! Might be evaluating an evil script!
var resp = eval("(" + xhr.responseText + ")");
// Or this if it's work
chrome.tabs.executeScript(tabs[0].id, {code: xhr.responseText});
}
}
xhr.send();
or you can use some library, $.get() with jquery or $http with angularjs.if you add eval in your code you must add in manifest.json this:
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"`,
这篇关于Chrome扩展程序:加载并执行外部脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!