覆盖/重叠XUL方法 [英] Overriding/Overlaying a XUL method

查看:136
本文介绍了覆盖/重叠XUL方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变Firefox拖放操作的行为(修复这个 WONTFIX bug ,并使用我的插件应用此旧的补丁) 。如何通过XUL覆盖这个方法?我试过这样的代码:

 <?xml version =1.0?> 
<!DOCTYPE覆盖SYSTEMchrome://dragdrop/locale/overlay.dtd>
< overlay id =ondrop-overlay
xmlns =http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul>

< binding id =tabbrowser>
<实施>
< method name =onDrop>
< body>
...
< / body>
< / method>
< / implementation>
< / binding>

< / overlay>



  overlay chrome://browser/content/tabbrowser.xml chrome://dragdrop/content/tabbrowser.xul 

在我的chrome.manifest,
中,但是我没有成功。请帮助我,谢谢:)解决方案覆盖只适用于XUL文档,而不是XBL绑定(这是一个XUL功​​能和不受XBL支持)。虽然你可以用你自己的继承自原始的绑定来替换 tabbrowser 元素的绑定,但是你的问题的更好的解决方案将会覆盖浏览器窗口在其上下文中运行您的代码。该代码将修改窗口的 tabbrowser 实例。所以在 chrome.manifest 你可以这样写:

  overlay chrome:/ /browser/content/browser.xul chrome://dragdrop/content/browserOverlay.xul 

browserOverlay.xul


$ b

 < ?xml version =1.0?> 
< overlay id =ondrop-overlay
xmlns =http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul>
< script type =application / x-javascriptsrc =browserOverlay.js/>
< / overlay>

最后, browserOverlay.js 会显示像这样:
$ b

window.addEventListener(load,function(event)
{
var browser = window.getBrowser();

//用自己的函数替换browser.onDrop方法,但保留原来的
//以后可以调用。
var origOnDrop = browser.onDrop;
browser.onDrop = function(aEvent,aXferData,aDragSession)
{
if(...)
{
//你自己的调用处理(开始搜索)
...
}
else
{
//让原始函数处理调用
origOnDrop.apply(this,arguments);
}
};
});

请注意,上面的代码避免使用全局变量,所有变量都在函数内声明。这通常是一个很好的行为,因为你正在浏览器窗口的上下文中运行 - 任何全局变量都与浏览器自己的代码共享,其他扩展的代码也在这个上下文中运行。两个代码为其全局变量选择相同的名称可能会导致讨厌的问题,而不是使用任何全局变量是避免这种情况的最好方法。

$ hr

编辑:您显然正在查看过时的代码(Gecko 1.8分支)。当前的代码没有 onDrop 方法,而是适当的事件处理程序。您不能替换事件处理程序 - 但您也不需要,因为如果拖动的文本不是链接,它将不会执行任何操作。所以你可以简单地添加你自己的事件处理程序来处理这种情况。


$ b

  window.addEventListener load,function(event)
{
var browser = window.getBrowser();
browser.tabContainer.addEventListener(drop,function(event)
{
if(...)
{
//这是一个搜索字符串,用它做一些事
...
//表示我们处理了事件
event.stopPropagation();
}
},false);
});


I want to change behavior of Firefox on drag and drop action (for fixing this WONTFIX bug and applying this a bit old patch with my addon). How I can override this method through XUL overlaying? I tried some codes like this:

<?xml version="1.0"?>
<!DOCTYPE override SYSTEM "chrome://dragdrop/locale/overlay.dtd">
<overlay id="ondrop-overlay"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 

      <binding id="tabbrowser">
          <implementation>
              <method name="onDrop">
                <body>
                ...
                </body>
              </method>
          </implementation>
      </binding>

</overlay>

While

overlay chrome://browser/content/tabbrowser.xml chrome://dragdrop/content/tabbrowser.xul

was in my chrome.manifest, but I had not any success. Please help me, thanks :)

解决方案

Overlays only apply to XUL documents, not to XBL bindings (it is a XUL feature and not supported by XBL). While you could replace the binding for the tabbrowser element by your own that inherits from the original binding, the better solution to your problem would be overlaying the browser window to run your code in its context. That code would then modify the tabbrowser instance of the window. So in chrome.manifest you would write:

overlay chrome://browser/content/browser.xul chrome://dragdrop/content/browserOverlay.xul

And in browserOverlay.xul:

<?xml version="1.0"?>
<overlay id="ondrop-overlay"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 
  <script type="application/x-javascript" src="browserOverlay.js"/>
</overlay>

And finally, browserOverlay.js would look like this:

window.addEventListener("load", function(event)
{
  var browser = window.getBrowser();

  // Replace browser.onDrop method by own function but keep the original so that it
  // can be called later.
  var origOnDrop = browser.onDrop;
  browser.onDrop = function(aEvent, aXferData, aDragSession)
  {
    if (...)
    {
      // Your own processing of the call (start a search)
      ...
    }
    else
    {
      // Let the original function handle the call
      origOnDrop.apply(this, arguments);
    }
  };
});

Note that the code above avoids using global variables, all variables are declared inside a function. This is generally a good course of action because you are running in the context of the browser window - any global variables are shared with the browser's own code and the code of other extensions also running in this context. Two pieces of code choosing the same name for their global variable could lead to nasty issues, not using any global variables is the best way to avoid this.


Edit: You were apparently looking at outdated code (Gecko 1.8 branch). The current code has no onDrop method but rather a proper event handler on the tab container element. You cannot replace an event handler - but you also don't need to because it will not do anything if the text dragged isn't a link. So you can simply add your own event handler that will handle this case.

window.addEventListener("load", function(event)
{
  var browser = window.getBrowser();
  browser.tabContainer.addEventListener("drop", function(event)
  {
    if (...)
    {
      // This is a search string, do something with it
      ...
      // Indicate that we processed the event
      event.stopPropagation();
    }
  }, false);
});

这篇关于覆盖/重叠XUL方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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