Addon-sdk打开带有预附加图像的新选项卡.html



在firefox插件中,我有main.js代码:

var button = ToggleButton({
  id: "ee-button",
  label: "EE button",
  icon: {
    "16": "./icon-16.png",
    "32": "./icon-32.png",
    "64": "./icon-64.png"
  },
  onChange: captureVisibleTab
});

在该文件中:(参考:选项卡屏幕截图)

function captureVisibleTab() {
  .
  . 
  .
  .
  var canvas = contentDocument.createElement('canvas');
  canvas.style.maxwidth = region.width + "px";
  canvas.style.maxheight = region.height + "px";
  canvas.width = region.width;
  canvas.height = region.height;
  var ctx = canvas.getContext('2d');
  ctx.clearRect(region.x, region.y, region.width, region.height);
  ctx.drawWindow(contentWindow, region.x, region.y, region.width, region.height, backgroundColor);
  var url = canvas.toDataURL(); 
  tabs.open({
  url: self.data.url("test.html"),
  isPinned: false,
  onOpen: function onOpen(tab) {
  },
  onReady: function(tab)
  {
    console.log('canvas : ' + (JSON.stringify(url)) );
    tab.attach({
    contentScript: 'document.body.style.border = "5px solid red"; document.body.appendChild("Test")'
    });
  }
  });  
  return url;
}

我需要打开一个新的选项卡,将此图像附加在html中
*已尝试:window.open=>未定义窗口
*已尝试:contentScript:'document.body.style.border="5px纯红";'=>作品
*已尝试:contentScript:'document.body.style.border="5px纯红";document.body.appendChild("Test")'=>错误:

  Message: TypeError: Argument 1 of Node.appendChild is not an object.
  Stack:   @javascript:document.body.style.border = "5px solid red"; document.body.appe
ndChild("Test"):1:47

对于url,只需将数据url与html一起使用即可:

var escapedHtml = escape('<b>rawr</b><img src="blah">')
var url = 'data:text/html,' + escapedHtml

或者你在栏里没有一个难看的url,把你的html变成一个blob,然后像这样加载blob url:

var {Blob, File} = Cu.import("resource://gre/modules/Services.jsm", {});
var oFileBody = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var oBlob = Blob([oFileBody], { type: "text/html"});
var blobUrl = URL.createObjectURL(oBlob); //returns a string like blob:null/bbe1b94e-0800-4af2-9d9e-df09c0b9cab3 so paste that into your url bar

相关内容

  • 没有找到相关文章

最新更新