【问题标题】:Communicating current tab URL to popup将当前选项卡 URL 与弹出窗口通信
【发布时间】:2014-03-07 16:20:09
【问题描述】:

我在 StackOverflow 上尝试了以下代码教程,但实际上无法在弹出窗口中接收活动标签 URL。

http://stackoverflow.com/questions/21043684/sending-data-from-popup-to-extension-js-not-working

http://stackoverflow.com/questions/21017681/how-to-send-data-from-popup-script-to-background-js-in-crossrider

我的目标是在用户进入该选项卡后立即在弹出窗口中接收活动选项卡 URL。这个 URL 是对需要从数据库中获取的数据的引用,因此对于我的插件来说是必不可少的。

如果能提供一个工作示例,我将不胜感激。

编辑

我现在可以在选项卡更改或 URL 更改时在网站的弹出窗口中获取 URL。但是,这对我没有用,因为一旦用户单击弹出窗口,我就需要 URL,这样我就可以获取活动选项卡的 URL 并对本地数据库运行查询以获取其中的数据。 我该怎么做?

谢谢!

【问题讨论】:

  • 我不明白。发布您现在如何获取 url 的示例代码。您的面板小部件。邮政编码,对你有好处。

标签: firefox-addon crossrider


【解决方案1】:

我不确定如何在 SDK 中做到这一点。

但您要做的是添加一个 TabSelect 事件侦听器。

这是在引导程序或覆盖插件中的完成方式。

function exampleTabSelected(event) {
  //tab was selected
  var tab = event.target; //this tab is the actual thingy you click in the tab bar at top
  var tabBrowser = tab.linkedBrowser; //this is the chrome browser within this tab
  var tabContentWindow = tabBrowser.contentWindow; //this is the HTML (or whatever type) contained inside the tab, this is where your website is
  var siteLocationObj = tabContentWindow.location;
  //location now includes properties like href, host, pathname, hash, and port
  //now put your the id of your id of the panel and you can do whatever to it
  var chromeWindow = tab.ownerGlobal; //this is the firefox browser window that the tab is in
  chromeWindow.document.getElementById('YOUR-PANEL-ID-HERE').querySelector('iframe').contentDocument.innerHTML = 'you are now on page: ' siteLocationObj.href;
}

// During initialisation
var container = gBrowser.tabContainer;
container.addEventListener("TabSelect", exampleTabSelected, false);

// When no longer needed
//container.removeEventListener("TabSelect", exampleTabSelected, false);

【讨论】:

    【解决方案2】:

    @infosec 通常,在弹出范围内,您无法直接确定选项卡何时变为活动状态。但是,在后台范围内可以使用Crossrider appAPI.tabs.onTabSelectionChanged 方法获取活动标签信息,然后使用appAPI.message.toPopup 将信息发送到弹出窗口。

    实现这一点的代码如下所示:

    background.js

    appAPI.ready(function($) {
        // Get the Active Tab information
        appAPI.tabs.onTabSelectionChanged(function(tabInfo) {
            // Send it to the popup
            appAPI.tabs.toPopup({tabInfo: tabInfo});
        });
    });
    

    crossriderMainpopup.html 中:

    function crossriderMain($) {
        appAPI.message.addListener(function(msg) {
            // Do something with the tab URL
            console.log('ActiveTab URL is ' + msg.tabInfo.tabUrl);
        });
    }
    

    [披露:我是 Crossrider 的员工]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2015-03-29
      • 1970-01-01
      相关资源
      最近更新 更多