fire/fox
火狐狸
火狐狸
  • UID32624
  • 注册日期2010-04-21
  • 最后登录2024-02-02
  • 发帖数172
  • 经验169枚
  • 威望0点
  • 贡献值182点
  • 好评度9点
  • 社区居民
  • 忠实会员
阅读:6050回复:3

[已解决]请问 userChromeJS 手势怎么写后台打开链接?

楼主#
更多 发布于:2021-12-08 17:59
这个是我现在用的:
// ==UserScript==
// @name                 Mousegestures.uc.js
// @namespace            Mousegestures@gmail.com
// @description          自定义鼠标手势
// @author               紫云飞&黒仪大螃蟹
// @homepageURL          http://www.cnblogs.com/ziyunfei/archive/2011/12/15/2289504.html
// @include              chrome://browser/content/browser.xhtml
// @include              chrome://browser/content/browser.xul
// @charset              UTF-8
// ==/UserScript==
(() => {
    'use strict';
    let ucjsMouseGestures = {
        lastX: 0,
        lastY: 0,
        directionChain: '',
        isMouseDownL: false,
        isMouseDownR: false,
        hideFireContext: false,
        shouldFireContext: false,
        GESTURES: {
            'L': {name: '后退', cmd: () => gBrowser.webNavigation.canGoBack && gBrowser.webNavigation.goBack()},
            'R': {name: '前进', cmd: () => gBrowser.webNavigation.canGoForward && gBrowser.webNavigation.goForward()},
            'UD': {name: '刷新页面', cmd: function() { document.getElementById("Browser:Reload").doCommand();}},
            'DU': {name: '转到主页', cmd: function() { BrowserHome(); }},
  
            'U': {name: '新建标签', cmd:  function() { BrowserOpenTab();  }},
  
            'DR': {name: '关闭当前标签', cmd: function(event) {            gBrowser.removeCurrentTab();        }},
            'DL': {name: '恢复关闭的标签', cmd:  function() { try {                document.getElementById('History:UndoCloseTab').doCommand();            } catch (ex) {                if ('undoRemoveTab' in gBrowser) gBrowser.undoRemoveTab();                else throw "Session Restore feature is disabled."            }        }  },
  
            'RU': {name: '转到页首', cmd: () => goDoCommand('cmd_scrollTop')},
            'RD': {name: '转到页尾', cmd: () => goDoCommand('cmd_scrollBottom')},
        },
  
        init: function() {
            let self = this;
            ['mousedown', 'mousemove', 'mouseup', 'contextmenu', 'DOMMouseScroll'].forEach(type => {
                gBrowser.tabpanels.addEventListener(type, self, true);
            });
            gBrowser.tabpanels.addEventListener('unload', () => {
                ['mousedown', 'mousemove', 'mouseup', 'contextmenu', 'DOMMouseScroll'].forEach(type => {
                    gBrowser.tabpanels.removeEventListener(type, self, true);
                });
            }, false);
        },
        handleEvent: function(event) {
            switch (event.type) {
            case 'mousedown':
                if (event.button == 2) {
                    (gBrowser.mPanelContainer || gBrowser.tabpanels).addEventListener("mousemove", this, false);
                    this.isMouseDownR = true;
                    this.hideFireContext = false;
                    [this.lastX, this.lastY, this.directionChain] = [event.screenX, event.screenY, ''];
                }
                if (event.button == 0) {
                    this.isMouseDownR = false;
                    this.stopGesture();
                }
                break;
            case 'mousemove':
                if (this.isMouseDownR) {
                    let[subX, subY] = [event.screenX - this.lastX, event.screenY - this.lastY];
                    let[distX, distY] = [(subX > 0 ? subX : (-subX)), (subY > 0 ? subY : (-subY))];
                    let direction;
                    if (distX < 10 && distY < 10) return;
                    if (distX > distY) direction = subX < 0 ? 'L' : 'R';
                    else direction = subY < 0 ? 'U' : 'D';
                    if (!this.xdTrailArea) {
                        this.xdTrailArea = document.createXULElement('hbox');
                        let canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
                        canvas.setAttribute('width', window.screen.width);
                        canvas.setAttribute('height', window.screen.height);
                        this.xdTrailAreaContext = canvas.getContext('2d');
                        this.xdTrailArea.style.cssText = '-moz-user-focus: none !important;-moz-user-select: none !important;display: -moz-box !important;box-sizing: border-box !important;pointer-events: none !important;margin: 0 !important;padding: 0 !important;width: 100% !important;height: 100% !important;border: none !important;box-shadow: none !important;overflow: hidden !important;background: none !important;opacity: 0.9 !important;position: fixed !important;z-index: 2147483647 !important; display: inline !important;';
                        this.xdTrailArea.appendChild(canvas);
                        gBrowser.selectedBrowser.parentNode.insertBefore(this.xdTrailArea, gBrowser.selectedBrowser.nextSibling);
                    }
                    if (this.xdTrailAreaContext) {
                        this.hideFireContext = true;
                        this.xdTrailAreaContext.strokeStyle = '#0065FF';
                        this.xdTrailAreaContext.lineJoin = 'round';
                        this.xdTrailAreaContext.lineCap = 'round';
                        this.xdTrailAreaContext.lineWidth = 3;
                        this.xdTrailAreaContext.beginPath();
                        this.xdTrailAreaContext.moveTo(this.lastX - gBrowser.selectedBrowser.screenX, this.lastY - gBrowser.selectedBrowser.screenY);
                        this.xdTrailAreaContext.lineTo(event.screenX - gBrowser.selectedBrowser.screenX, event.screenY - gBrowser.selectedBrowser.screenY);
                        this.xdTrailAreaContext.closePath();
                        this.xdTrailAreaContext.stroke();
                        this.lastX = event.screenX;
                        this.lastY = event.screenY;
                    }
                    if (direction != this.directionChain.charAt(this.directionChain.length - 1)) {
                        this.directionChain += direction;
                        StatusPanel._label = this.GESTURES[this.directionChain] ? '手势: ' + this.directionChain + ' ' + this.GESTURES[this.directionChain].name : '未知手势:' + this.directionChain;
                    }
                }
                break;
            case 'mouseup':
                if (this.isMouseDownR && event.button == 2) {
                    if (this.directionChain) this.shouldFireContext = false;
                    this.isMouseDownR = false;
                    this.directionChain && this.stopGesture();
                }
                break;
            case 'contextmenu':
                if (this.isMouseDownR || this.hideFireContext) {
                    this.shouldFireContext = true;
                    this.hideFireContext = false;
                    event.preventDefault();
                    event.stopPropagation();
                }
                break;
            case 'DOMMouseScroll':
                if (this.isMouseDownR) {
                    this.shouldFireContext = false;
                    this.hideFireContext = true;
                    this.directionChain = 'W' + (event.detail > 0 ? '+' : '-');
                    this.stopGesture();
                }
                break;
            }
        },
        stopGesture: function() {
            if (this.GESTURES[this.directionChain]) this.GESTURES[this.directionChain].cmd();
            if (this.xdTrailArea) {
                this.xdTrailArea.parentNode.removeChild(this.xdTrailArea);
                this.xdTrailArea = null;
                this.xdTrailAreaContext = null;
            }
            this.directionChain = '';
            setTimeout(() => StatusPanel._label = '', 2000);
            this.hideFireContext = true;
        }
    };
    ucjsMouseGestures.init();
})();

最新喜欢:

aunsenaunsen 视线之外视线之外 edit_checkedit_c...
fire/fox
火狐狸
火狐狸
  • UID32624
  • 注册日期2010-04-21
  • 最后登录2024-02-02
  • 发帖数172
  • 经验169枚
  • 威望0点
  • 贡献值182点
  • 好评度9点
  • 社区居民
  • 忠实会员
1楼#
发布于:2021-12-09 11:38
找到了,alice 的手势有后台打开,但是没有轨迹显示。。。
https://github.com/alice0775/userChrome.js
哪位大佬能改一下紫云飞这个吗?这个好像很久没有更新了。
edit_check
小狐狸
小狐狸
  • UID57451
  • 注册日期2020-02-02
  • 最后登录2022-10-22
  • 发帖数3
  • 经验3枚
  • 威望0点
  • 贡献值0点
  • 好评度2点
2楼#
发布于:2021-12-23 17:04
//==UserScript==
// @name            Mousegestures.uc.js
// @description  Mouse gestures
// @include      chrome://browser/content/browser.xhtml
// @charset      UTF-8
//==/UserScript==
 
(() => {
        'use strict';
        let ucjsMouseGestures = {
            lastX: 0,
            lastY: 0,
            directionChain: '',
            isMouseDownL: false,
            isMouseDownR: false,
            hideFireContext: false,
            shouldFireContext: false,
            GESTURES: {
                'L': {name: 'Back', cmd: () => gBrowser.webNavigation.canGoBack && gBrowser.webNavigation.goBack()},
                'R': {name: 'Forward', cmd: () => gBrowser.webNavigation.canGoForward && gBrowser.webNavigation.goForward()},
                'UD': {name: 'Reload', cmd: function() { document.getElementById("Browser:Reload").doCommand();}},
                'DU': {name: 'Home', cmd: function() { BrowserHome(); }},
 
                'U': {name: 'Open Link', cmd:  function() { gBrowser.selectedBrowser.messageManager.sendAsyncMessage('chromeToContent', 'open-link'); }},
                'DR': {name: 'CloseCurrentTab', cmd: function(event) { gBrowser.removeCurrentTab(); }},
                'DL': {name: 'UndoCloseTab', cmd:  function() { try { document.getElementById('History:UndoCloseTab').doCommand(); } catch (ex) { if ('undoRemoveTab' in gBrowser) gBrowser.undoRemoveTab(); else throw "Session Restore feature is disabled." } } },
 
                'RU': {name: 'GoTop', cmd: () => goDoCommand('cmd_scrollTop')},
                'RD': {name: 'GoBottom', cmd: () => goDoCommand('cmd_scrollBottom')},
            },
 
            init: function() {
                let self = this;
                ['mousedown', 'mousemove', 'mouseup', 'contextmenu', 'DOMMouseScroll'].forEach(type => {
                    gBrowser.tabpanels.addEventListener(type, self, true);
                });
                gBrowser.tabpanels.addEventListener('unload', () => {
                    ['mousedown', 'mousemove', 'mouseup', 'contextmenu', 'DOMMouseScroll'].forEach(type => {
                        gBrowser.tabpanels.removeEventListener(type, self, true);
                    });
                }, false);
                Services.mm.loadFrameScript(this.frameScript, true);
                Services.mm.addMessageListener('contentToChrome', this.chromeListener);
            },
 
            chromeListener: function (message) {
                const { document, gBrowser } = message.target.ownerGlobal;
                const { cmd, url } = message.data;
 
                switch (cmd) {
                    case 'newTab-focus':
                        if(url) {
                            let nuevoTab = gBrowser.addTab(url, { owner: gBrowser.selectedTab, relatedToCurrent: true, triggeringPrincipal: gBrowser.selectedBrowser.contentPrincipal });
                            gBrowser.selectedTab = nuevoTab; //Delete this line if wanna background tab
                        }
                    break;
                }
            },
 
            frameScript: 'data:application/javascript;charset=UTF-8,' +
                encodeURIComponent('(' + (function () {
                    let clickedElement;
                    var GetLink = function (e) {
                        ImgSrc = undefined;
                        LnkSrc = undefined;
                        clickedElement = e.target;
                        switch (clickedElement.tagName)  {
                            case 'IMG': //Clikeo sobre una imagen
                                ImgSrc = clickedElement.src || clickedElement.currentSrc;   //La imagen
                                LnkSrc = clickedElement.parentElement.href || clickedElement.currentSrc;    //El link donde va la imagen (url o img)
                            break;
                            case 'AREA': //Deberia ser imagen
                                let uM = clickedElement.ownerDocument.querySelector('[usemap]');
                                if (uM && uM.useMap == '#' + clickedElement.parentElement.name) ImgSrc = uM.src;
                            break;
                            default: //Caso generico
                                try {
                                    let Elmnt = clickedElement;
                                    LnkSrc = Elmnt.href;
                                    if (!LnkSrc) {
                                        for (let pb=0; pb<6; pb++) { //Try max 6 levels up
                                            Elmnt = Elmnt.parentNode;
                                            LnkSrc = Elmnt.href;
                                            if(LnkSrc) break;
                                        }
                                    }
                                } catch(e) { LnkSrc = undefined; }
                            break;
                        }
                    }
                    addEventListener('mousedown', GetLink, true);
                    contentListener = function (msg) {
                        switch (msg.data) {
                            case 'open-link':
                                if(typeof LnkSrc != 'undefined') sendAsyncMessage('contentToChrome', {cmd: 'newTab-focus', url: LnkSrc});
                            break;
                        }
                    }
                    addMessageListener('chromeToContent', contentListener);
            }).toString() + ')();'),
 
            handleEvent: function(event) {
                switch (event.type) {
                case 'mousedown':
                    if (event.button == 2) {
                        (gBrowser.mPanelContainer || gBrowser.tabpanels).addEventListener("mousemove", this, false);
                        this.isMouseDownR = true;
                        this.hideFireContext = false;
                        [this.lastX, this.lastY, this.directionChain] = [event.screenX, event.screenY, ''];
                    }
                    if (event.button == 0) {
                        this.isMouseDownR = false;
                        this.stopGesture();
                    }
                    break;
                case 'mousemove':
                    if (this.isMouseDownR) {
                        let[subX, subY] = [event.screenX - this.lastX, event.screenY - this.lastY];
                        let[distX, distY] = [(subX > 0 ? subX : (-subX)), (subY > 0 ? subY : (-subY))];
                        let direction;
                        if (distX < 10 && distY < 10) return;
                        if (distX > distY) direction = subX < 0 ? 'L' : 'R';
                        else direction = subY < 0 ? 'U' : 'D';
                        if (!this.xdTrailArea) {
                            this.xdTrailArea = document.createXULElement('hbox');
                            let canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
                            canvas.setAttribute('width', window.screen.width);
                            canvas.setAttribute('height', window.screen.height);
                            this.xdTrailAreaContext = canvas.getContext('2d');
                            this.xdTrailArea.style.cssText = '-moz-user-focus: none !important;-moz-user-select: none !important;display: -moz-box !important;box-sizing: border-box !important;pointer-events: none !important;margin: 0 !important;padding: 0 !important;width: 100% !important;height: 100% !important;border: none !important;box-shadow: none !important;overflow: hidden !important;background: none !important;opacity: 0.9 !important;position: fixed !important;z-index: 2147483647 !important; display: inline !important;';
                            this.xdTrailArea.appendChild(canvas);
                            gBrowser.selectedBrowser.parentNode.insertBefore(this.xdTrailArea, gBrowser.selectedBrowser.nextSibling);
                        }
                        if (this.xdTrailAreaContext) {
                            this.hideFireContext = true;
                            this.xdTrailAreaContext.strokeStyle = '#0065FF';
                            this.xdTrailAreaContext.lineJoin = 'round';
                            this.xdTrailAreaContext.lineCap = 'round';
                            this.xdTrailAreaContext.lineWidth = 3;
                            this.xdTrailAreaContext.beginPath();
                            this.xdTrailAreaContext.moveTo(this.lastX - gBrowser.selectedBrowser.screenX, this.lastY - gBrowser.selectedBrowser.screenY);
                            this.xdTrailAreaContext.lineTo(event.screenX - gBrowser.selectedBrowser.screenX, event.screenY - gBrowser.selectedBrowser.screenY);
                            this.xdTrailAreaContext.closePath();
                            this.xdTrailAreaContext.stroke();
                            this.lastX = event.screenX;
                            this.lastY = event.screenY;
                        }
                        if (direction != this.directionChain.charAt(this.directionChain.length - 1)) {
                            this.directionChain += direction;
                            StatusPanel._label = this.GESTURES[this.directionChain] ? 'Gesto: ' + this.directionChain + ' ' + this.GESTURES[this.directionChain].name : 'Desconocido:' + this.directionChain;
                        }
                    }
                    break;
                case 'mouseup':
                    if (this.isMouseDownR && event.button == 2) {
                        if (this.directionChain) this.shouldFireContext = false;
                        this.isMouseDownR = false;
                        this.directionChain && this.stopGesture();
                    }
                    break;
                case 'contextmenu':
                    if (this.isMouseDownR || this.hideFireContext) {
                        this.shouldFireContext = true;
                        this.hideFireContext = false;
                        event.preventDefault();
                        event.stopPropagation();
                    }
                    break;
                case 'DOMMouseScroll':
                    if (this.isMouseDownR) {
                        this.shouldFireContext = false;
                        this.hideFireContext = true;
                        this.directionChain = 'W' + (event.detail > 0 ? '+' : '-');
                        this.stopGesture();
                    }
                    break;
                }
            },
            stopGesture: function() {
                if (this.GESTURES[this.directionChain]) this.GESTURES[this.directionChain].cmd();
                if (this.xdTrailArea) {
                    this.xdTrailArea.parentNode.removeChild(this.xdTrailArea);
                    this.xdTrailArea = null;
                    this.xdTrailAreaContext = null;
                }
                this.directionChain = '';
                setTimeout(() => StatusPanel._label = '', 2000);
                this.hideFireContext = true;
            }
        };
        ucjsMouseGestures.init();
})();
fire/fox
火狐狸
火狐狸
  • UID32624
  • 注册日期2010-04-21
  • 最后登录2024-02-02
  • 发帖数172
  • 经验169枚
  • 威望0点
  • 贡献值182点
  • 好评度9点
  • 社区居民
  • 忠实会员
3楼#
发布于:2021-12-23 17:54
edit_check://==UserScript==
// @name            Mousegestures.uc.js
// @description     Mouse gestures
// @include         chrom...
回到原帖
这个太强了,手势轨迹显示和后台打开标签都有了。
注释或删除54行,就是后台标签打开链接。
游客

返回顶部