Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将来自外部域的js执行到新选项卡中_Javascript_Google Chrome Extension - Fatal编程技术网

Javascript 将来自外部域的js执行到新选项卡中

Javascript 将来自外部域的js执行到新选项卡中,javascript,google-chrome-extension,Javascript,Google Chrome Extension,这是我的第一个问题 我使用的是chrome扩展,它应该能够获取页面的整个DOM(从当前选项卡),并将其重新注入到新选项卡中。到目前为止,除了js文件之外,一切都很好。对文件的调用正确地写入了我在新选项卡中注入的DOM中,但看起来它们没有被执行(甚至没有根据Chrome的开发工具中的“Source”选项卡下载) 是否有我忘记的许可,或者是CSP的原因?如果是第二种情况,有没有办法解决这个问题 这是舱单: { "name": "CS DomReady Checker", "version":

这是我的第一个问题

我使用的是chrome扩展,它应该能够获取页面的整个DOM(从当前选项卡),并将其重新注入到新选项卡中。到目前为止,除了js文件之外,一切都很好。对文件的调用正确地写入了我在新选项卡中注入的DOM中,但看起来它们没有被执行(甚至没有根据Chrome的开发工具中的“Source”选项卡下载)

是否有我忘记的许可,或者是CSP的原因?如果是第二种情况,有没有办法解决这个问题

这是舱单:

{
  "name": "CS DomReady Checker",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get the DOM Ready",
  "browser_action": {
    "default_icon": "assets/img/icon.png",
    "default_popup": "popup.html"
  },
  "permissions": ["tabs", "<all_urls>", "webNavigation"],
  "icons": { 
    "128": "assets/img/icon-128.png" 
  }
}
getPageSource.js:

function DOMtoString(document_root) {
    var html = '<base href="'+window.location.href+'">',
        node = document_root.firstChild;
    while (node) {
        switch (node.nodeType) {
        case Node.ELEMENT_NODE:
            html += node.outerHTML;
            break;
        case Node.TEXT_NODE:
            html += node.nodeValue;
            break;
        case Node.CDATA_SECTION_NODE:
            html += '<![CDATA[' + node.nodeValue + ']]>';
            break;
        case Node.COMMENT_NODE:
            html += '<!--' + node.nodeValue + '-->';
            break;
        case Node.DOCUMENT_TYPE_NODE:
            // (X)HTML documents are identified by public identifiers
            html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
            break;
        }
        node = node.nextSibling;
    }
    return html;
}
//Send message to popup.js
chrome.runtime.sendMessage({
    action: "getSource",
    source: DOMtoString(document),
    url: window.location.href
});
在一个随机网站上,我发现两个错误:

拒绝从中加载插件数据 '' 因为它违反了以下内容安全策略指令: 对象src'self'blob:文件系统:“

无法在“DOMWindow”:目标源上执行“postMessage” 提供的(“”)与收件人不匹配 窗的起源 ('铬-extension://fdkkfiefjneekgpgnlobbmdnmanoeolf")


谢谢您的帮助。

我认为您应该在“web可访问资源”中添加js文件,如下所示:

{
 "name": "CS DomReady Checker",
 "version": "1.0",
 "manifest_version": 2,
 "description": "Get the DOM Ready",
 "browser_action": {
   "default_icon": "assets/img/icon.png",
   "default_popup": "popup.html"
  },
 "permissions": ["tabs", "<all_urls>", "webNavigation"],
 "icons": { 
   "128": "assets/img/icon-128.png" 
  },
 "web_accessible_resources": [ "js/youjsfile.js ", "js/another.js"]
}
{
“名称”:“CS DomReady Checker”,
“版本”:“1.0”,
“清单版本”:2,
“描述”:“准备好DOM”,
“浏览器操作”:{
“默认图标”:“assets/img/icon.png”,
“默认弹出窗口”:“popup.html”
},
“权限”:[“选项卡”、“网页导航”],
“图标”:{
“128”:“assets/img/icon-128.png”
},
“网络可访问资源”:[“js/youjsfile.js”,“js/other.js”]
}

如何重新注入html?你有错误吗?我添加了一些关于脚本和错误的详细信息。问题是我的脚本完成了这项工作。在这里,我需要的脚本是由我的新标签运行的网站调用。我不知道我是否真的很清楚…:/
//Intercept message from popup.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    if(request.action === 'injectHtml') {
        //Inject the sniffed dom into the new tab
        document.documentElement.innerHTML = request.source;

        sendResponse({response: true});
    }

});
{
 "name": "CS DomReady Checker",
 "version": "1.0",
 "manifest_version": 2,
 "description": "Get the DOM Ready",
 "browser_action": {
   "default_icon": "assets/img/icon.png",
   "default_popup": "popup.html"
  },
 "permissions": ["tabs", "<all_urls>", "webNavigation"],
 "icons": { 
   "128": "assets/img/icon-128.png" 
  },
 "web_accessible_resources": [ "js/youjsfile.js ", "js/another.js"]
}