简体   繁体   English

在chrome扩展名中捕获URL

[英]capture URL in chrome extension

Try to capture the url chrome browser using chrome extension background script. 尝试使用Chrome扩展程序后台脚本捕获chrome浏览器的网址。

I try below code is working fine for new url change but not working in tab change 我尝试下面的代码对于新的URL更改工作正常,但在选项卡更改中无法正常工作

background.js background.js

chrome.tabs.getSelected(null, function(tab) {
           chrome.extension.getBackgroundPage().console.log(tab.url);
});

manifest.json 的manifest.json

{
  "manifest_version": 2,

  "name": "My Test Extension",
  "description": "This extension demonstrates.",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
      "tabs",
       "activeTab",
        "http://*/*",
        "https://*/*"
  ]
}

console log print once when it install after that no response. 控制台日志打印一次,安装后即无反应。

I want to capture URL of current tab... when user change the browser tab or create new tab I need to capture the url of the current browser tab in background without click extension icons. 我想捕获当前选项卡的URL ...当用户更改浏览器选项卡或创建新选项卡时,我需要在后台捕获当前浏览器选项卡的URL,而无需单击扩展名图标。

After get the URL I need to append one query string at the end of the URL. 获取网址后,我需要在网址末尾附加一个查询字符串。

for example 例如

current tab url : stackoverflow.com extension change/append url : stackoverflow.com/question/java 当前标签网址:stackoverflow.com扩展名更改/追加网址:stackoverflow.com/question/java

below code working fine as expected 下面的代码按预期工作正常

chrome.tabs.onUpdated.addListener( function( tabId,  changeInfo,  tab) {
       chrome.extension.getBackgroundPage().console.log(tab.url);
       if(tab.url=="https://www.google.co.in/"){
            chrome.tabs.update(tab.id, {url: 'https://www.yahoo.com/'});
       }
});

let me know the comments 让我知道评论

Listen for the onUpdated event. 监听onUpdated事件。

chrome.tabs.onUpdated.addListener(function(integer tabId, object changeInfo, Tab tab) {...});

http://developer.chrome.com/extensions/tabs.html#event-onUpdated http://developer.chrome.com/extensions/tabs.html#event-onUpdated

1) Event for when the active tab is changed: 1)更改活动选项卡时的事件:

chrome.tabs.onActivated.addListener(function(object activeInfo) {...});

From documentation : 从文档

Fires when the active tab in a window changes. 窗口中的活动选项卡更改时触发。 Note that the tab's URL may not be set at the time this event fired, but you can listen to onUpdated events to be notified when a URL is set. 请注意,触发此事件时可能未设置选项卡的URL,但是设置URL时,您可以侦听要通知的onUpdated事件。

2) Most likely, you also need to listen to the chrome.windows.onFocusChanged event which is fired when the currently focused window is changed. 2)最有可能的是,您还需要监听chrome.windows.onFocusChanged事件,该事件在更改当前焦点窗口时触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM