Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Middle click launcher
- // @include main
- // ==/UserScript==
- if (typeof window === "undefined" || globalThis !== window) {
- const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
- if (!Services.appinfo.remoteType) {
- this.EXPORTED_SYMBOLS = ["MiddleClickLauncherParent"];
- ChromeUtils.defineModuleGetter(this, "FileUtils", "resource://gre/modules/FileUtils.jsm");
- try {
- ChromeUtils.registerWindowActor("MiddleClickLauncher", {
- parent: {
- moduleURI: __URI__,
- },
- child: {
- moduleURI: __URI__,
- events: {
- mousedown: {},
- },
- },
- allFrames: true,
- messageManagerGroups: ["browsers"],
- matches: [`*://*/*`],
- });
- } catch (e) {Cu.reportError(e);}
- this.MiddleClickLauncherParent = class extends JSWindowActorParent {
- processWithPath(path) {
- try {
- let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
- process.init(new FileUtils.File(path));
- return process;
- } catch (e) {
- Cu.reportError(e);
- }
- return null;
- }
- receiveMessage({name, data}) {
- switch(name) {
- case "MCL:LaunchWithSelectionText":
- try {
- const process = this.processWithPath("c:\\windows\\system32\\cmd.exe");
- const args = ["/k", "echo", data.selectionText];
- if (process) {
- process.runw(false, args, args.length);
- }
- } catch (e) {
- Cu.reportError(e);
- }
- break;
- }
- }
- };
- }
- else {
- this.EXPORTED_SYMBOLS = ["MiddleClickLauncherChild"];
- this.MiddleClickLauncherChild = class extends JSWindowActorChild {
- handleEvent(event) {
- if (event.type !== "mousedown") return;
- if (event.button !== 1 || event.detail !== 1) return;
- const selection = this.contentWindow.getSelection();
- const selectionText = selection.toString();
- if (selectionText) {
- event.preventDefault();
- this.sendAsyncMessage("MCL:LaunchWithSelectionText", {selectionText});
- }
- }
- };
- }
- }
- else {
- try {
- const fileHandler = Services.io.getProtocolHandler("file").QueryInterface(Ci.nsIFileProtocolHandler);
- const scriptFile = fileHandler.getFileFromURLSpec(Components.stack.filename);
- const resourceHandler = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
- if (!resourceHandler.hasSubstitution("middle-click-launcher-ucjs")) {
- resourceHandler.setSubstitution("middle-click-launcher-ucjs", Services.io.newFileURI(scriptFile.parent));
- }
- ChromeUtils.import(`resource://middle-click-launcher-ucjs/${scriptFile.leafName}?${scriptFile.lastModifiedTime}`);
- } catch (e) {Cu.reportError(e)}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement