如何在打开弹出窗口时获取当前标签网址[无标签权限] [英] How to get current tab url while opening the popup [without tabs permission]

查看:123
本文介绍了如何在打开弹出窗口时获取当前标签网址[无标签权限]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在chrome 31.0.1650.57上有一个browserAction扩展测试。我希望在用户点击扩展程序图标时获取有效的标签页URL,保存并稍后使用它。



使用标签页时,我可以做到这一点权限。但是这个许可有很大的权力(我不需要),用户也在抱怨这一点。



我按照文章切换到 activeTab 权限 http://developer.chrome.com/extensions/activeTab 但它只适用当没有弹出。正如您可以在 make_page_red 例子。

 
chrome.browserAction.onClicked.addListener(函数(标签){
//不需要标签或主机权限!
console.log(★tab.url,tab.url);
});

您可以通过更改 make_page_red 示例清单文件添加一个默认弹出窗口,如下所示:

 
{
name:Page Redder,
description:使当前页面变红,
version:2.0,
permissions:[
activeTab
],
background:{
scripts:[background.js],
持久性:false
},
browser_action:{
default_title:使此页面变红,
default_popup:popup.html← - -----------------

},
manifest_version:2
}

现在因为我有一个默认的弹出窗口,所以browserAction.onClicked侦听器不再被调用。





谢谢



popup.js

  chrome.tabs.query({active:true,currentWindow:true },function(tabArray){
console.log(tabArray [0] .url);
});

当然,还有 popup.html 使用脚本标记加载 popup.js 。 (而且我不确定你在做什么后台脚本。)



编辑:



在除上述之外,我的扩展还有两个其他文件。 manifest.json 是从您的复制品中删除的,但背景部分已被删除。唯一的其他文件是 popup.html (来自内存,请耐心等待):

 < html>< body>< script type =text / javascriptsrc =popup.js>< / script>< / body>< / html> 

要查看控制台,我单击浏览器动作,它将显示一个50像素的方形窗口。右键点击检查元素,然后点击控制台查看网址。


I have a browserAction extension testing on chrome 31.0.1650.57. I want to get active tab url when the user click on the extension icon, save it and use it later.

I am able to do that when using tabs permission. But this permission has a lot of power (that I don't need) and users are complaining about this.

I followed the article to switch to activeTab permission http://developer.chrome.com/extensions/activeTab but it only works when there is no popup. As you can see in the make_page_red example.

chrome.browserAction.onClicked.addListener(function(tab) {
  // No tabs or host permissions needed!
  console.log("★ tab.url", tab.url);
});

You can check by changing the make_page_red example manifest file to add a default popup like this:

{
  "name": "Page Redder",
  "description": "Make the current page red",
  "version": "2.0",
  "permissions": [
    "activeTab"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Make this page red",
    "default_popup": "popup.html"  ←-------------------

  },
  "manifest_version": 2
}

Now because I have a default popup the browserAction.onClicked listener is not called anymore.

How can I get current tab url and still have my popup opening?

Thanks

解决方案

browserAction.onClicked is not called anymore (as you noted), so just skip that:

popup.js:

chrome.tabs.query({active:true,currentWindow:true},function(tabArray){
    console.log(tabArray[0].url);
});

And of course, have popup.html load popup.js with a script tag. (And I'm not sure what you're background script is doing.)

Edit:

In addition to the above, my extension has two other files. manifest.json is copied from yours, but has the background section deleted. The only other file is popup.html (from memory, so bear with me):

<html><body><script type="text/javascript" src="popup.js"></script></body></html>

To view the console, I click the browser action, which brings up a 50 pixel square window. Right click on that, "inspect element", and click console to view the url.

这篇关于如何在打开弹出窗口时获取当前标签网址[无标签权限]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆