2012-08-29 173 views
1

this.window.location.href工作是不是在HTML文件瀏覽器擴展程序 工作我試圖在腳本中此功能:window.location.href無法在Chrome擴展

function myFunction() 
{ 
    var pl = this.window.location.href; 
    var sWords= localStorage.getItem(pl); 
    document.write(pl); 
} 

,這讓我:

chrome-extension://ebeadbfnnghmakkbimckpdmocjffkbjc/popup.html 

所以我應該做什麼來獲得頁面的鏈接?

+0

的可能重複[如何得到我的頁面動作彈出當前打開的選項卡上的網址是什麼?(http://stackoverflow.com/questions/10413911/how-to-get-the-currently -opened-tabs-url-in-my-page-action-popup) –

+0

@RobW鏈接刪除..謝謝指針..對不起... – ManseUK

回答

2

您可以通過chrome.tabs.query方法獲取當前選定的選項卡。你需要通過兩個選項:

  1. currentWindow : true
  2. active : true

它將返回符合條件的翼片陣列。您可以從那裏獲取網址。就像這樣:

chrome.tabs.query(
    { 
     currentWindow: true, // currently focused window 
     active: true   // selected tab 
    }, 
    function (foundTabs) { 
     if (foundTabs.length > 0) { 
      var url = foundTabs[0].url; // <--- this is what you are looking for 
     } else { 
      // there's no window or no selected tab 
     } 
    } 
);