Jos*_*osh 7 javascript tabs google-chrome google-chrome-extension
做了一些研究之后,我提出的代码就是:
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)
});
});
Run Code Online (Sandbox Code Playgroud)
这是在我的弹出式html文件中调用的javascript文件中.但它不显示当前网站的URL,而是显示任何内容.
我在这个和其他网站上发现了很多关于这个的帖子,但是我还没有能够实现任何所谓的解决方案.
任何帮助将不胜感激.
PAE*_*AEz 28
也许这就是你要找的......
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
alert(tabs[0].url);
}
);
Run Code Online (Sandbox Code Playgroud)
use*_*r12 14
我遇到过同样的问题.我写了这个扩展名来显示当前用户正在弹出窗口中浏览的URL.
manifest.js
"permissions": [
"tabs"
]
Run Code Online (Sandbox Code Playgroud)
popup.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);
});
});
Run Code Online (Sandbox Code Playgroud)