0

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 が変更されたときに、Web サイトのポップアップで URL を取得できるようになりました。ただし、ユーザーがポップアップをクリックするとすぐに URL が必要になるため、アクティブなタブの URL を取得し、ローカル データベースに対してデータのクエリを実行できるため、これは役に立ちません。これどうやってするの?

ありがとう!

4

2 に答える 2

1

@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});
    });
});

popup.htmlcrossriderMain :

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

[開示:私はクロスライダーの従業員です]

于 2014-03-09T05:08:22.570 に答える
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);
于 2014-03-08T08:59:35.527 に答える