xKr*_*ipz 7 javascript google-chrome google-chrome-extension
我正在创建我的第一个Chrome扩展程序,我需要一些帮助.我认为一切正常,除了我无法获得标签的当前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);
// Open the page up.
chrome.tabs.create(
{
"url" : finalUrl
}
);
});
Run Code Online (Sandbox Code Playgroud)
谁能帮帮我吗?谢谢.
编辑:
谢谢您的回复.我通过移动工作
var finalUrl = "http://example.com/";
finalUrl += encodeURI(siteUrl);
// Open the page up.
chrome.tabs.create(
{
"url" : finalUrl
}
Run Code Online (Sandbox Code Playgroud)
内
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var siteUrl = tabs[0].url;
});
Run Code Online (Sandbox Code Playgroud)
Mik*_*e M 13
chrome.tabs.getCurrent(function(tab){
alert(tab.url);
});
Run Code Online (Sandbox Code Playgroud)
或者,如果您在内容脚本中,
alert(document.location.href);
Run Code Online (Sandbox Code Playgroud)
您需要的信息已经在onClicked监听器的回调中提供给您.
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;
Run Code Online (Sandbox Code Playgroud)
例如,你可以达到你想要的效果:
manifest.json的:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"permissions": ["contextMenus"]
}
Run Code Online (Sandbox Code Playgroud)
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)
});
}
});
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您使用的是内容脚本,则可以使用
document.location.href
Run Code Online (Sandbox Code Playgroud)
document.location 是Object,可以在URL中提供一组有用的块
document.location.host返回域名ex:" http://www.google.com/ "document.location.path 返回域名后的部分document.location.hash 在URL中的#符号后面返回任何内容| 归档时间: |
|
| 查看次数: |
13756 次 |
| 最近记录: |