Lio*_*nel 5 google-chrome-extension
我在popup.html中使用带有按钮的chrome扩展程序,打开一个新标签页.新选项卡的目标URL将当前(原始)选项卡的URL保存为参数.
例如:当从中触发时http://stackoverflow.com/,新选项卡应该具有类似的URLhttp://www.mydestination.com/index.php?url=http://stackoverflow.com/
这是我的js:
document.addEventListener('DOMContentLoaded', function (tab) {
document.getElementById('button').addEventListener("click", function(tab) {
chrome.tabs.create({url: 'http://www.mydestination.com/index.php?url=' + tab.url});
});
})
Run Code Online (Sandbox Code Playgroud)
新选项卡完全打开,但URL为http://www.mydestination.com/index.php?url=undefined(url = undefined).
我认为manifest.json拥有正确的权限:
{
"manifest_version": 2,
"name": "My project",
"version" : "1.7",
"browser_action": {
"default_icon" : "img/icon.png",
"default_title" : "My project",
"default_popup": "html/main.html"
},
"permissions": [
"tabs"
],
"icons": {
"16": "img/icon.png"
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何正确运输网址的任何线索?
问题是当前选项卡是您的chrome弹出窗口.在这种情况下,您没有有效的URL.您必须选择标签.为此,您可以使用chrome.tabs.query.选择当前窗口和活动标签:
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('button').addEventListener("click", function () {
chrome.tabs.query({
'active': true,
'windowId': chrome.windows.WINDOW_ID_CURRENT
}, function (tabs) {
chrome.tabs.create({
url: 'http://www.mydestination.com/index.php?url=' + tabs[0].url
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3596 次 |
| 最近记录: |