Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  2.  
  3. //Cu.import("resource://gre/modules/PopupNotifications.jsm");
  4. Cu.import("resource://gre/modules/Services.jsm");
  5.  
  6. var AutoFocusUrlbar = {
  7. cs: Services.console, // nsIConsoleService
  8. ww: Services.ww, // nsIWindowWatcher
  9. wm: Services.wm, // nsIWindowMediator
  10. ps: Services.prefs,
  11.  
  12. get doc() {
  13. return this.wm.getMostRecentWindow("navigator:browser").document;
  14. },
  15.  
  16. get eres() {
  17. return this.ps.getCharPref('extensions.AutoFocusUrlbar.excludeChar');
  18. },
  19.  
  20. handleEvent: function (e) {
  21. var document = this.doc;
  22. var elm = document.activeElement;
  23. while (1) {
  24. if (elm == null) return;
  25. if (elm.localName == "browser" || elm.localName == "iframe" ||
  26. elm.localName == "frame") {
  27. elm = elm.contentDocument.activeElement;
  28. continue;
  29. }
  30. break;
  31. }
  32. if (elm.ownerDocument.designMode == 'on' ||
  33. elm.localName == "input" || elm.localName == "textarea" ||
  34. elm.localName == "select" ||
  35. elm.isContentEditable) return;
  36. var mod = e.ctrlKey || e.metaKey || e.altKey;
  37. if (mod) return;
  38. var c = String.fromCharCode(e.charCode);
  39. var excludePattern;
  40. try {
  41. var excludeRegExpString = this.eres;
  42. excludePattern = new RegExp(excludeRegExpString);
  43. } catch (err) {
  44. excludePattern = /[^a-zA-Z]/;
  45. }
  46. if (excludePattern.test(c)) return;
  47. var urlbar = document.getElementById("urlbar");
  48. if (!urlbar) return;
  49. e.preventDefault();
  50. urlbar.focus();
  51. try {
  52. urlbar.value = '';
  53. var evt = document.createEvent("KeyboardEvent");
  54. evt.initKeyEvent('keypress', false, true, null,
  55. false, false, false, false, 0, e.charCode);
  56. var input = urlbar.inputField;
  57. input.dispatchEvent(evt);
  58. } catch (err) {
  59. urlbar.value = c;
  60. }
  61. },
  62.  
  63. aListener: {
  64. onOpenWindow: function (aWindow) {
  65. var win = aWindow.docShell.QueryInterface(Ci
  66. .nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
  67. win.addEventListener("load", function () {
  68. win.removeEventListener("load", arguments.callee, true);
  69. if (win.document.documentElement.getAttribute("windowtype") !=
  70. "navigator:browser") return;
  71. win.addEventListener("keypress", AutoFocusUrlbar, false);
  72. }, true);
  73. },
  74. onCloseWindow: function (aWindow) {},
  75. onWindowTitleChange: function (aWindow, aTitle) {},
  76. },
  77.  
  78. startup: function () {
  79. this.wm.addListener(this.aListener);
  80. var cw = this.wm.getEnumerator("navigator:browser");
  81. while (cw.hasMoreElements()) {
  82. var win = cw.getNext().QueryInterface(Ci.nsIDOMWindow);
  83. win.addEventListener("keypress", this, false);
  84. }
  85. },
  86. shutdown: function () {
  87. this.wm.removeListener(this.aListener);
  88. var cw = this.wm.getEnumerator("navigator:browser");
  89. while (cw.hasMoreElements()) {
  90. var win = cw.getNext().QueryInterface(Ci.nsIDOMWindow);
  91. win.removeEventListener("keypress", this, false);
  92. }
  93. }
  94. }
  95.  
  96.  
  97. // 启用
  98. function startup(data, reason) {
  99. var cs = Services.console;
  100. AutoFocusUrlbar.startup();
  101. }
  102.  
  103. // 禁用或应用程序退出
  104. function shutdown(data, reason) {
  105. AutoFocusUrlbar.shutdown();
  106. }
  107.  
  108. // 安装
  109. function install(data, reason) {
  110. }
  111.  
  112. // 卸载
  113. function uninstall(data, reason) {
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement