Javascript 在 chrome 扩展中显示当前 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11493995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display current URL in a chrome extension
提问by Josh
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)
});
});
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.
这是在我的弹出 html 文件中调用的 javascript 文件中。但是,它不会显示当前网站的 URL,而是什么都不显示。
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.
我在这个网站和其他网站上找到了多篇关于此的帖子,但我无法实施任何假设的解决方案。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by PAEz
Maybe this is what your looking for....
也许这就是你要找的......
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
alert(tabs[0].url);
}
);
回答by user12
I had the same issue. I wrote this extension to display the current URL user is browsing now in the popup.
我遇到过同样的问题。我写了这个扩展来在弹出窗口中显示当前用户正在浏览的 URL。
manifest.js
manifest.js
"permissions": [
"tabs"
]
popup.js
弹出窗口.js
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
callback(url);
});
}
function renderURL(statusText) {
document.getElementById('status').textContent = statusText;
}
document.addEventListener('DOMContentLoaded', function() {
getCurrentTabUrl(function(url) {
renderURL(url);
});
});
回答by Anonimi
Is this what you are looking for?
这是你想要的?
window.location.href