簡體   English   中英

如何從Firefox插件打開彈出窗口(document.write不起作用)

[英]How to open popup from Firefox addon (document.write is not working)

我希望能夠在注入腳本完成后從我的Firefox插件中將彈出窗口打開到新選項卡中,或進行其他操作。 注入的腳本從當前選項卡中顯示的打開網頁獲取html數據。 此代碼可在Google Chrome瀏覽器中使用。 不幸的是,由於新打開的選項卡沒有任何HTML,因此無法在Firefox中使用。

這是我的代碼

|

main.js

var self = require("sdk/self");
var tabs = require("sdk/tabs");

var button = require("sdk/ui/button/action").ActionButton({
  id: "style-tab",
  label: "Style Tab",
  icon: "./icon-16.png",
  onClick: function() {
    worker = tabs.activeTab.attach({
      contentScriptFile: self.data.url("getPagesSource.js")
    });
    worker.port.emit("getSource");
  }
});

getPagesSource.js

//Do some Node.js stuff here and store into the "newHTML" variable

var newHTML = "<html><body><p>70 Lines of HTML<p></body></html>";

var myWindow = window.open("", "_blank");
myWindow.document.write(newHTML);

self.port.on("getSource", function() {
  DOMtoString(document);
});

之后,我在控制台中收到以下錯誤:

console.error:我的附件:消息:錯誤:拒絕訪問屬性“文檔”的權限

要保持方法不變,請嘗試以下操作:

var myWindow = window.open('data:text/html,<b>hi</b> <i>italics</i>', '_blank');

另一個選擇是創建一個文件並打開該文件:

要創建一個htm文件,然后在新窗口中打開,請執行以下操作:

var { Cu } = require('chrome');
Cu.import('resource://gre/modules/osfile.jsm');

var pathToWriteFile = OS.Path.join(OS.Constants.Path.tmpDir, 'GetPagesSource.htm');
var promise_writeTempFile = OS.File.writeAtomic(pathToWriteFile, '<html>blah blah</html>', {encoding:'utf-8'});
promise_writeTempFile.then(
  function onSuccess() {
    var myWindow = window.open(OS.Path.toFileURI(pathToWriteFile), '_blank');
  },
  function onFail(aReason) {
    console.error('promise_writeTempFile failed, aReason:', aReason);
  }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM