Firefox add-on get the tab body content

后端 未结 3 682
余生分开走
余生分开走 2020-12-20 07:54

Hello everyone i have an question about firefox add-on:

How i can get the body content from a tab, for example.

var content = require(\"tabs\").activ         


        
相关标签:
3条回答
  • 2020-12-20 08:29

    You can try this:

    var tabs = require("sdk/tabs");
    var { getTabForId, getTabContentWindow } = require ("sdk/tabs/utils");
    var tab = require("tabs").activeTab;
    var window = getTabContentWindow (getTabForId(tab.id));
    var content = window.document.body.innerHTML;
    

    But maybe this answer is better.

    0 讨论(0)
  • 2020-12-20 08:29

    You can get the body of the currently-selected tab using the following (after DOMContentLoaded):

    gBrowser.contentDocument.body.innerHTML
    

    Note: This only works in a standard extension, not in the SDK.

    0 讨论(0)
  • 2020-12-20 08:32

    The Add-on SDK doesn't allow direct access to the tab contents - the idea is that the add-on and the tab might end up living in different processes eventually. What you do is injecting a content script into the tab to get you the necessary data, something like this:

    var tab = require("tabs").activeTab;
    tab.attach({
      contentScript: "self.postMessage(document.body.innerHTML);",
      onMessage: function(data)
      {
        console.log("Tab data received: " + data);
      }
    });
    
    0 讨论(0)
提交回复
热议问题