我正在尝试运行以下命令:
chrome.tabs.onCreated.addListener(function (tab){
if (tab.url.indexOf(".salesforce.com/") != -1 || tab.url.indexOf(".force.com/") != -1) {
chrome.tabs.executeScript(tab.id, {
"file": "loadScript.js"
}, function () {
console.log("Script Executed .. ");
});
} else {
var wrongTab = chrome.i18n.getMessage("wrongTab");
console.log(wrongTab);
alert(wrongTab);
}
});
从理论上讲,哪个应在页面加载时运行loadScript.js文件...。loadScript.js文件如下所示,这应将文件追加到正在运行的页面,而不是当前的后台页面。 :
/* Create a scriipt element in head of HTML and put /soap/ajax/31.0/connection.js in the src */
var connectJsUrl = "/connection.js";
function loadScript(url, callback) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = url;
var done = false;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
callback();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
loadScript(connectJsUrl, function() {
console.log("Script Confirmed...")
});
/* Check to see if the file have been appended correctly and works correctly */
var JSFile = "chrome-extension://" + window.location.host + connectJsUrl;
var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
if (req == null) {
console.log("Error: XMLHttpRequest failed to initiate.");
};
req.onload = function() {
try {
eval(req.responseText);
} catch (e) {
console.log("There was an error in the script file.");
}
};
try {
req.open("GET", JSFile, true);
req.send(null);
} catch (e) {
console.log("Error retrieving data httpReq. Some browsers only accept cross-domain request with HTTP.");
};
我仍然是Chrome扩展程序和.js的新手,如果我犯了一个愚蠢的错误,请原谅:)
我从中得到的是以下内容:拒绝将字符串评估为JavaScript,因为在以下内容安全策略指令中不允许使用'unsafe-eval'脚本来源:“ script-src'self'chrome-extension-resource :“。
为了防止跨站点脚本编写,Google禁止了eval功能。
要解决此问题,请将此代码添加到manifest.json
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
如果需要进一步说明,请发表评论
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句