访问用户当前从扩展中查看的每个文档 [英] Accessing every document that a user currently views from an extension

查看:126
本文介绍了访问用户当前从扩展中查看的每个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个扩展,用于检查用户对某些数据结构进行查看的每个文档,执行一些后端服务器调用,并将结果显示为对话框。问题是使用事件侦听器正确启动和继续序列。我的实际想法是:

  Load:function()
{
var Listener = function() Fabogore.Start();};
var ListenerTab = window.gBrowser.selectedTab;
ListenerTab.addEventListener(load,Listener,true);
}
(...)
ListenerTab.removeEventListener(load,Listener,true);
Fabogore.Load();

当浏览器打开时,首先初始化Fabogore.Load函数。只有一旦我获得这些数据结构,而不是之后它才起作用。但理论上脚本应该初始化一个新的监听器,所以也许是selectedTab。我也试着听重点事件。



如果有人有一个替代解决方案,如何访问用户正在查看的页面,我也会感到舒适。

解决方案

好的,我发现了一种从MDN文档中工作的方法,并且实现了用户打开的每个文档都可以被你的扩展名访问。访问用户所关注的每个文档太多,我希望代码只能执行一次。所以我从初始化Exentsion开始,并且收听DOMcontentloaded事件

  window.addEventListener(load,function(){Fabogore .init();},false); 


var Fabogore = {
init:function(){
var appcontent = document.getElementById(appcontent); // browser
if(appcontent)
appcontent.addEventListener(DOMContentLoaded,Fabogore.onPageLoad,true);
},

每次加载页面时,都会执行该代码。现在重要的是,您使用新加载的页面执行代码,而不是旧代码。您可以使用变量aEvent:

  onPageLoad:function(aEvent)
{
var doc = aEvent.originalTarget; //发起事件的文档

使用变量doc可以检查数据结构使用XPCNativeWrapper等感谢Wladimir带我正确的方向,我想如果你需要一个更复杂的事件聆听选择他的方式与进度听众。


I'm writing an extension that checks every document a user views on certain data structures, does some back-end server calls and displays the results as a dialog.The problem is starting and continuing the sequence properly with event listeners. My actual idea is:

Load: function()
{
var Listener = function(){ Fabogore.Start();};
var ListenerTab = window.gBrowser.selectedTab;
ListenerTab.addEventListener("load",Listener,true);
}
(...)
ListenerTab.removeEventListener("load", Listener, true);
Fabogore.Load();

The Fabogore.Load function is first initialized when the browser gets opened. It works only once I get these data structures, but not afterwards. But theoretically the script should initialize a new listener, so maybe it's the selectedTab. I also tried listening to focus events.

If someone has got an alternative solution how to access a page a user is currently viewing I would feel comfortable as well.

解决方案

Ok, I found a way which works from the MDN documentation, and achieves that every document a user opens can be accessed by your extension. Accessing every document a user focuses is too much, I want the code to be executed only once. So I start with initializing the Exentsion, and Listen to DOMcontentloaded Event

window.addEventListener("load", function() { Fabogore.init(); }, false);


var Fabogore = {
init: function() {
var appcontent = document.getElementById("appcontent");   // browser
if(appcontent)
  appcontent.addEventListener("DOMContentLoaded", Fabogore.onPageLoad, true);
},

This executes the code every Time a page is loaded. Now what's important is, that you execute your code with the new loaded page, and not with the old one. You can acces this one with the variable aEvent:

onPageLoad: function(aEvent)
{
var doc = aEvent.originalTarget;//Document which initiated the event

With the variable "doc" you can check data structures using XPCNativeWrapper etc. Thanks Wladimir for bringing me in the right direction, I suppose if you need a more sophisticated event listening choose his way with the progress listeners.

这篇关于访问用户当前从扩展中查看的每个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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