2012-08-29 128 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 
     } 
    } 
);