Get nsIDOMWindow of a Tab to do Text Input

萝らか妹 提交于 2019-12-12 00:42:33

问题


Is it possible to use Text Input Processor on a specific Tab of a Firefox window?

Do all the tabs share the same nsIDOMWindow object? If so, then is there any other solution?

Rephrasing my problem: I want to type text to a specific tab, and if there is more than one, then it might not be the current.

Below is an example code to write text on any Firefox window, but only for current tab:

function myTestFunc() {
  // var windows = Services.wm.getEnumerator("navigation:browser");
  var windows = require("sdk/windows");
  for (let browserWindow of windows.browserWindows)  {
    //console.log(window.title);
    var chromeWindow = viewFor(browserWindow);
    console.log(chromeWindow.document.location.href);
    var idomWindow = chromeWindow.QueryInterface(Ci.nsIDOMWindow);

    var TIP = Cc["@mozilla.org/text-input-processor;1"].
              createInstance(Ci.nsITextInputProcessor);
    if(!TIP.beginInputTransaction(idomWindow, onNotifyImpl))  {
      console.log("Error TIP can't start");
      continue;
    }
    TIP.setPendingCompositionString("foo-bar-buzz");
    if (!TIP.flushPendingComposition()) {
      console.log("Failed to flush");
      continue; // Composition is not started
    }
  }

}

回答1:


nsIDOMWindow (+ various related interfaces like the docshell) is the XPCOM representation of regular window objects in the w3c specs. And since window objects can be nested (e.g. via iframes) so can be nsIDOMWindows. When you're accessing browser windows you're generally accessing the outer windows representing the browser chrome, not the content.

In principle you can access a tab's content window directly from its <browser> XUL element, but to be forward-compatible with e10s you should use framescripts instead.



来源:https://stackoverflow.com/questions/37510020/get-nsidomwindow-of-a-tab-to-do-text-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!