问题描述
我正在创建我的第一个Chrome扩展程序,我需要一些帮助。 II认为一切正常,除了我无法获得该标签的当前URL。
var menu = chrome。 contextMenus.create({
title:extension,
contexts:[all]
});
chrome.tabs.query({'active':true,'lastFocusedWindow':true},function(tabs){
var siteUrl = tabs [0] .url;
});
chrome.contextMenus.onClicked.addListener(function(activeTab)
{
chrome.tabs.query({'active':true,'lastFocusedWindow': true},function(tabs){
var siteUrl = tabs [0] .url;
});
var finalUrl =http://example.com/ ;
finalUrl + = encodeURI(siteUrl);
//打开页面
chrome.tabs.create(
{
url:finalUrl
}
);
});
任何人都可以帮我吗?谢谢。
编辑:
感谢您的回复。我通过移动来实现它。
var finalUrl =http://example.com/;
finalUrl + = encodeURI(siteUrl);
//打开页面。
chrome.tabs.create(
{
url:finalUrl
}
里面
chrome.tabs.query({'active':true,'lastFocusedWindow': true},function(tabs){
var siteUrl = tabs [0] .url;
});
$ b $您需要的信息已经在 。
chrome.contextMenus.onClicked.addListener(函数(info,tab){
//选项卡的URL(如果有的话)
var tabURL = tab&& tab.url;
//页面的URL(如果菜单没有在框架中触发)
var pageURL = info.pageUrl;
//框架的URL(如果菜单是在框架中触发的)
var frameURL = info。 frameUrl;
你可以达到你想要的效果:
$ b
manifest.json:
<$ p
manifest_version:2,
name:Test Extension,
version:0.0,
background:{
persistent:false,
scripts:[background.js]
},
permissions :[contextMenus]
}
background.js:
var baseURL ='http://example.com/';
chrome.contextMenus.create({
id:'myMenu',//< - 事件页面需要ID
标题:'做很酷的东西',
上下文:['all']
},function(){
/ *寻找错误总是个好主意* /
if(chrome.runtime.lastError) {
alert('ERROR:'+ chrome.runtime.lastError.message);
}
});
chrome.contextMenus.onClicked.addListener(function(info,tab){
/ *检查触发了哪个上下文菜单* /
if(info.menuItemId === 'myMenu'){
/ *获取框架的URL或者(如果没有)网页* /
var currentURL = info.frameUrl || info.pageUrl;
/ *打开一个新标签* /
chrome.tabs.create({
url:baseURL + encodeURI(currentURL)
});
}
});
I'm creating my first Chrome extension and I need some help. I I think everything is working except the fact that I can't get the current URL of the tab.
var menu = chrome.contextMenus.create({
"title": "extension",
"contexts": ["all"]
});
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var siteUrl = tabs[0].url;
});
chrome.contextMenus.onClicked.addListener(function(activeTab)
{
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var siteUrl = tabs[0].url;
});
var finalUrl = "http://example.com/";
finalUrl += encodeURI(siteUrl);
// Open the page up.
chrome.tabs.create(
{
"url" : finalUrl
}
);
});
Can anyone please help me? Thanks.
EDIT:
Thank you for your replies. I got it working by moving
var finalUrl = "http://example.com/";
finalUrl += encodeURI(siteUrl);
// Open the page up.
chrome.tabs.create(
{
"url" : finalUrl
}
Inside
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var siteUrl = tabs[0].url;
});
The info you require are provided to you already in the callback of the onClicked
listener.
chrome.contextMenus.onClicked.addListener(function(info, tab) {
// The URL of the tab (if any)
var tabURL = tab && tab.url;
// The URL of the page (if the menu wasn't triggered in a frame)
var pageURL = info.pageUrl;
// The URL of the frame (if the menu was triggered in a frame)
var frameURL = info.frameUrl;
E.g. you could achieve what you want like this:
manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"permissions": ["contextMenus"]
}
background.js:
var baseURL = 'http://example.com/';
chrome.contextMenus.create({
id: 'myMenu', // <-- event-pages require an ID
title: 'Do cool stuff',
contexts: ['all']
}, function () {
/* It is always a good idea to look for errors */
if (chrome.runtime.lastError) {
alert('ERROR: ' + chrome.runtime.lastError.message);
}
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
/* Check which context-menu was triggered */
if (info.menuItemId === 'myMenu') {
/* Get the URL of the frame or (if none) the page */
var currentURL = info.frameUrl || info.pageUrl;
/* Open a new tab */
chrome.tabs.create({
url: baseURL + encodeURI(currentURL)
});
}
});
这篇关于从chrome.contextMenus.onClicked侦听器中获取当前网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!