经过一些研究,我想出的代码是这样的:
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,而是什么也不显示。
我在这个网站和其他网站上找到了多篇关于此的帖子,但我无法实现任何假定的解决方案。
如有任何帮助,我们将不胜感激。
最佳答案
也许这就是您要找的......
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
alert(tabs[0].url);
}
);
tabs权限需要在manifest中设置...
list .json
"permissions": [
"tabs"
]
关于javascript - 在 chrome 扩展中显示当前 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493995/