检测Firefox插件中的选项卡URL更改 [英] Detect tab URL change inside a Firefox add-on

查看:129
本文介绍了检测Firefox插件中的选项卡URL更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具体来说,我需要检测URL何时发生变化,但是有一个扩展在Chrome上运行,可以监视URL的变化。没有新的页面加载或导航。有些网站会这样做(例如,当您点击查看YouTube上的另一个视频时)。



在Chrome上,我通过以下方式完成了这个功能:

  chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
if(changeInfo&& .status ==complete){
// do stuff here
}
});

如何检测Firefox插件中的这种变化?



我被告知要使用:听取所有标签上的事件,但我不能把它放在一起。其中一个问题是在扩展中未定义 gBrowser



我做错了什么?

有没有简单的方法?



要安装侦听器,请将SDK选项卡转换为其原始(旧)表示使用viewFor。
使用modelFor和getTabForContentWindow可以向后转换。

  const tabs = require(sdk / tabs); 
const {viewFor} = require('sdk / view / core');
const {modelFor} = require('sdk / model / core');
const {getBrowserForTab,getTabForContentWindow} = require(sdk / tabs / utils);
const {Ci,Cu} = require(chrome);
Cu.import(resource://gre/modules/XPCOMUtils.jsm,this);

var progressListener = {
QueryInterface:XPCOMUtils.generateQI([Ci.nsIWebProgressListener,Ci.nsISupportsWeakReference]),
onLocationChange:function(aProgress,aRequest,aURI){
var highLevel = modelFor(getTabForContentWindow(aProgress.DOMWindow));
console.log(onLocationChange,highLevel.url);
}
};
$ b tabs.on('open',function(newTab){
var lowLevel = viewFor(newTab);
var browser = getBrowserForTab(lowLevel);
browser .addProgressListener(progressListener);
});

不要忘记在扩展卸载时删除监听器。 Tab监听器被自动删除,但ProgressListeners不会被启动。




转换成chrome窗口


I have an extension, functional on Chrome, that monitors the active Tab for URL changes.

Specifically, I need to detect when the URL changes, but there is no new page load or navigation. Some sites do this (e.g. when you click to view another video on YouTube).

On Chrome, I accomplished this with:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo && changeInfo.status == "complete") {
        //do stuff here
    }
});

How do I detect such changes in a Firefox add-on?

I've been told to use: Listening to events on all tabs, but I couldn't put it together. One of the problems was that gBrowser was not defined in the extension.

What am I doing wrong?

Is there a simpler way?

解决方案

Use ProgressListener to be notified about location changes.

To install a listener, convert SDK tab to its raw (old) representation using viewFor. Backward conversion is possible with modelFor and getTabForContentWindow.

const tabs = require("sdk/tabs");
const {viewFor} = require('sdk/view/core');
const {modelFor} = require('sdk/model/core');
const {getBrowserForTab, getTabForContentWindow} = require("sdk/tabs/utils");
const {Ci, Cu} = require("chrome");
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);

var progressListener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]),
    onLocationChange: function(aProgress, aRequest, aURI) {
        var highLevel= modelFor(getTabForContentWindow(aProgress.DOMWindow));
        console.log("onLocationChange ", highLevel.url);
    }
};

tabs.on('open', function(newTab) {
    var lowLevel = viewFor(newTab);
    var browser = getBrowserForTab(lowLevel);
    browser.addProgressListener(progressListener);
});

Don't forget to remove listeners on extension unload. Tab listeners are removed automagically, but ProgressListeners won't be.

Inspired by Converting to chrome windows

这篇关于检测Firefox插件中的选项卡URL更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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