在 chrome 扩展中显示当前 URL [英] Display current URL in a chrome extension

查看:50
本文介绍了在 chrome 扩展中显示当前 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番研究,我想出的代码是这样的:

After doing some research, the code that I have come up with is this:

var outUrl;
// first get the windowid
chrome.windows.getCurrent(function(window) {
    // then get the current active tab in that window
    chrome.tabs.query({
        active: true,
        windowId: window.id
    }, function (tabs) {
        var tab = tabs[0];
        document.write(tab.url)
    });
});

这是在从我的弹出 html 文件调用的 javascript 文件中.但是它不会显示当前网站的 URL,而是什么都不显示.

This is in a javascript file which is called from my popup html file. It does not, however display the URL of the current website, instead it displays nothing.

我在这个网站和其他网站上发现了多篇关于此的帖子,但我无法实施任何假设的解决方案.

I have found multiple posts about this on this and other websites but I haven't been able to implement any of the supposed solutions.

任何帮助将不胜感激.

推荐答案

也许这就是您要找的....

Maybe this is what your looking for....

chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
   function(tabs){
      alert(tabs[0].url);
   }
);

并且标签权限需要在清单中设置...

And the tabs permission needs to be set in the manifest...

manifest.json

manifest.json

"permissions": [ 
  "tabs"
]

这篇关于在 chrome 扩展中显示当前 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆