Javascript Firefox扩展:打开窗口并写入动态内容

Javascript Firefox扩展:打开窗口并写入动态内容,javascript,browser,google-chrome-extension,tabs,firefox-addon,Javascript,Browser,Google Chrome Extension,Tabs,Firefox Addon,我开发了一个Chrome扩展,它与FirefoxWebExtensionsAPI基本兼容。只有一个问题: 在Chrome扩展中,我有popup.js和background.js。用户点击一个按钮,popup.js会将chrome.sendMessage发送到background.js,在那里接收数据,然后(popup.html可能会同时关闭)我只需调用background.js: newWin = window.open("about:blank", "Document Query", "wid

我开发了一个Chrome扩展,它与FirefoxWebExtensionsAPI基本兼容。只有一个问题:

在Chrome扩展中,我有popup.js和background.js。用户点击一个按钮,popup.js会将chrome.sendMessage发送到background.js,在那里接收数据,然后(popup.html可能会同时关闭)我只需调用background.js:

newWin = window.open("about:blank", "Document Query", "width=800,height=500");
newWin.document.open();
newWin.document.write('<html><body><pre>' + documentJson + '</pre></body></html>');
// newWin.document.close();
background.js (创建了local output.html,并在Manifest.json-tabs、activeTab、output.html、about:blank中授予了多个权限)


如何从background.js将数据导入新窗口/弹出窗口-我可以从那里打开一个空页面,因此只需运行executeScript()

,感谢@wOxxOm为我指出一个数据URI,将json文档从background.js传输到浏览器中

在搜索javascript方法以构建数据URI时,我发现了以下线程,建议创建:

所以我的解决方案是:

background.js
现代方法是将数据传递到专用页面。顺便说一句,executeScript对您没有帮助,因为它只用于内容脚本,而这些脚本用于网页,而不是扩展页面。谢谢您的回答!我研究了“现代方法”,我需要解决方案4。(背景页)。仍然希望有一些简单的解决方案,因为我现在有一个三线做这项工作。同样感谢executeScript的解释,API文档并没有那么精确地说明它仅用于内容脚本-但现在我看到了它们给出的“提示”,这解释了为什么我无法让它工作。您可能可以使用一个包含完整html的数据URI,而不是关于:blank.Awesome@wOxxOm!!这正是我所希望的简单解决方案——我从来没有想过:)我要发布我问题的答案。如果你愿意,我想给你一点赏金,请检查dashdevs.org并加入discord,我在那里(这里没有办法PM)
    // working in firefox and chrome (popup.js)
    const newWin = window.open("about:blank", "hello", "width=200,height=200");
    newWin.document.write("Hello, world!");

    // not working firefox: id's match, he enters function (newWindow) but document.write doing nothing (but no error in log)
    // not working chrome: doesnt even enter "function (newWindow)""
    chrome.windows.create({
      type: 'popup',
      url: "output.html"
    }, function (newWindow) {
      console.log(newWindow);
      console.log(newWindow.id);
      chrome.tabs.executeScript(newWindow.tabs[0].id, {
        code: 'document.write("hello world");'
      });
    });
            // opening but executeScript not working in firefox: Unchecked lastError value: Error: The operation is insecure.
            // opening but executeScript not working in chrome: Unchecked runtime.lastError: Cannot access contents of url "chrome-extension://plhphckppghaijagdmghdnjpilpdidkh/output.html". Extension manifest must request permission to access this host
            chrome.tabs.create({
              // type: 'popup',
              url: "output.html"
            }, function (newWindow) {
              console.log(newWindow);
              console.log(newWindow.id);
              chrome.tabs.executeScript(newWindow.id, {
                code: 'document.write("hello world");'
              });
            });
var documentJson = JSON.stringify(documents, null, 2)
let a = URL.createObjectURL(new Blob([documentJson]))
chrome.windows.create({
  type: 'popup',
  url: a
});