【问题标题】:window.location.href not working in chrome extension [duplicate]window.location.href 在 chrome 扩展中不起作用 [重复]
【发布时间】:2012-08-29 09:48:14
【问题描述】:

this.window.location.href 在 chrome 扩展中不工作 在 html 文件中,我在脚本中尝试了这个函数:

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

它给了我:

chrome-extension://ebeadbfnnghmakkbimckpdmocjffkbjc/popup.html

那么我应该怎么做才能获得页面的链接?

【问题讨论】:

  • @RobW 链接已删除 .. 感谢您的指点 .. 抱歉 ...

标签: javascript url google-chrome-extension


【解决方案1】:

您可以通过chrome.tabs.query 方法获取当前选中的标签。您需要传递两个选项:

  1. currentWindow : true
  2. active : true

它将返回一个符合条件的选项卡数组。您可以从那里获取 URL。像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-04
    • 2015-06-26
    • 1970-01-01
    • 2013-12-24
    • 2017-05-26
    • 2012-01-22
    • 2012-08-08
    • 1970-01-01
    相关资源
    最近更新 更多