Advertisement
Guest User

Untitled

a guest
Apr 20th, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Middle click launcher
  3. // @include        main
  4. // ==/UserScript==
  5.  
  6. if (typeof window === "undefined" || globalThis !== window) {
  7.     const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
  8.     if (!Services.appinfo.remoteType) {
  9.         this.EXPORTED_SYMBOLS = ["MiddleClickLauncherParent"];
  10.         ChromeUtils.defineModuleGetter(this, "FileUtils", "resource://gre/modules/FileUtils.jsm");
  11.         try {
  12.             ChromeUtils.registerWindowActor("MiddleClickLauncher", {
  13.                 parent: {
  14.                     moduleURI: __URI__,
  15.                 },
  16.                 child: {
  17.                     moduleURI: __URI__,
  18.                     events: {
  19.                         mousedown: {},
  20.                     },
  21.                 },
  22.                 allFrames: true,
  23.                 messageManagerGroups: ["browsers"],
  24.                 matches: [`*://*/*`],
  25.             });
  26.         } catch (e) {Cu.reportError(e);}
  27.  
  28.         this.MiddleClickLauncherParent = class extends JSWindowActorParent {
  29.             processWithPath(path) {
  30.                 try {
  31.                     let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  32.                     process.init(new FileUtils.File(path));
  33.                     return process;
  34.                 } catch (e) {
  35.                     Cu.reportError(e);
  36.                 }
  37.                 return null;
  38.             }
  39.             receiveMessage({name, data}) {
  40.                 switch(name) {
  41.                     case "MCL:LaunchWithSelectionText":
  42.                         try {
  43.                             const process = this.processWithPath("c:\\windows\\system32\\cmd.exe");
  44.                             const args = ["/k", "echo", data.selectionText];
  45.                             if (process) {
  46.                                 process.runw(false, args, args.length);
  47.                             }
  48.                         } catch (e) {
  49.                             Cu.reportError(e);
  50.                         }
  51.                         break;
  52.                 }
  53.             }
  54.         };
  55.     }
  56.     else {
  57.         this.EXPORTED_SYMBOLS = ["MiddleClickLauncherChild"];
  58.         this.MiddleClickLauncherChild = class extends JSWindowActorChild {
  59.             handleEvent(event) {
  60.                 if (event.type !== "mousedown") return;
  61.                 if (event.button !== 1 || event.detail !== 1) return;
  62.                 const selection = this.contentWindow.getSelection();
  63.                 const selectionText = selection.toString();
  64.                 if (selectionText) {
  65.                     event.preventDefault();
  66.                     this.sendAsyncMessage("MCL:LaunchWithSelectionText", {selectionText});
  67.                 }
  68.             }
  69.         };
  70.     }
  71. }
  72. else {
  73.     try {
  74.         const fileHandler = Services.io.getProtocolHandler("file").QueryInterface(Ci.nsIFileProtocolHandler);
  75.         const scriptFile = fileHandler.getFileFromURLSpec(Components.stack.filename);
  76.         const resourceHandler = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
  77.         if (!resourceHandler.hasSubstitution("middle-click-launcher-ucjs")) {
  78.             resourceHandler.setSubstitution("middle-click-launcher-ucjs", Services.io.newFileURI(scriptFile.parent));
  79.         }
  80.         ChromeUtils.import(`resource://middle-click-launcher-ucjs/${scriptFile.leafName}?${scriptFile.lastModifiedTime}`);
  81.     } catch (e) {Cu.reportError(e)}
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement