Handlers Helper

Helper for protocol_hook.lua

当前为 2023-09-18 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Handlers Helper
// @namespace   Violentmonkey Scripts
// @include       http*://*/*
// @grant       none
// @version     1.3
// @author      -
// @description Helper for protocol_hook.lua
// ==/UserScript==

function attachDrag(elem) {

function GM_btoaUrl(url) {
    return btoa(url).replace(/\//g, "_").replace(/\+/g, "-").replace(/\=/g, "");
}

    function EA(attr, type) {
        console.log(attr, type);
        var url;
        if (attr.startsWith('http')) {
            url = attr;
        }
        if (url == '') {
            url = location.href;
        }
        var subs = '';
        var s = url;

            var app = 'play';
            if (type != 'vid') {
                var app = type.toLowerCase();
            }
            var bs = GM_btoaUrl(s);
            var url2 = 'mpv://' + app + '/' + bs + '/' + "?referer=" + GM_btoaUrl(location.href);
            if (subs != '') {
                url2 = url2 + '?subs=' + GM_btoaUrl(subs);
            }
            //alert(url2);
            location.href = url2;
    }

    function getDirection(x, y, cx, cy) {
        /*=================
        |                 |
        | 1↖   2↑   3↗ |
        |                 |
        | 4←    5    6→ |
        |                 |
        | 7↙   8↓   9↘ |
        |                 |
        |=================*/
        let d, t;
        if ((cx - x) >= -50 && (cx - x) <= 50 && (cy - y) >= -50 && (cy - y) <= 50) {return 5;}
        // Change (4 == 4) to (8 == 4) to enable 8 directions
        if (4 == 4) { //4 directions
            if (Math.abs(cx - x) < Math.abs(cy - y)) {
                d = cy > y ? "8" : "2";
            } else {
                d = cx > x ? "6" : "4";
            }
        } else { //8 directions
            t = (cy - y) / (cx - x);
            if (-0.4142 <= t && t < 0.4142) d = cx > x ? '6' : "4";
            else if (2.4142 <= t || t < -2.4142) d = cy > y ? '8' : '2';
            else if (0.4142 <= t && t < 2.4142) d = cx > x ? '9' : '1';
            else d = cy > y ? '7' : '3';
        }
        return d;
    }
    elem.addEventListener('dragstart', function (e) {
        //console.log(e.target);
        //console.log(e.target.shadowRoot);
        /*if (e.target.nodeName != "A") {
        e.stopPropagation();
        e.stopImmediatePropagation();
        //e.preventDefault();
        }*/
        console.log('dragstart');
        var x1 = e.clientX;
        var y1 = e.clientY;
        var dragend = elem.addEventListener('dragend', function doEA(e) {
            var x2 = e.clientX;
            var y2 = e.clientY;
            var direction = getDirection(x1, y1, x2, y2);
            //if ((x2 - x1) >= -50 && (x2 - x1) <= 50 && (y2 - y1) >= -50 && (y2 - y1) <= 50) {direction = 5;console.log(5);}
            //if (e.target.nodeName == "A" && e.target.href.match(/youtube.com|youtu.be|streamable.com/)) {
            if (direction == 6) { //right
                console.log('MPV :' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                EA(e.target.href, 'vid');
            } else if (direction == 4) { //left
                console.log('Streamlink: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                EA(e.target.href, 'stream');
            } else if (direction == 2) { //up
                console.log('Open: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                window.open(e.target.href, '_blank');
            } else if (direction == 8) { //down
                console.log('YTDL: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                EA(e.target.href, 'ytdl');
            } else if (direction == 1) { //up left
                console.log('DUMMY: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                //EA(e.target.href, 'ytdl');
            } else if (direction == 3) { //up right
                console.log('DUMMY: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                //EA(e.target.href, 'ytdl');
            } else if (direction == 7) { //down left
                console.log('DUMMY: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                //EA(e.target.href, 'ytdl');
            } else if (direction == 9) { //down right
                console.log('DUMMY: ' + e.target.href);
                console.log(x1, y1, x2, y2, direction);
                //EA(e.target.href, 'ytdl');
            }
            //}
            console.log(direction);
            this.removeEventListener('dragend', doEA);
        }, false);
    }, false);
}

attachDrag(document);
var attachedeles = [];
document.addEventListener('mouseover', function(e) {
  if (e.target.shadowRoot) {
    if (attachedeles.includes(e.target) == false) {
      console.log(attachedeles);
      attachedeles.push(e.target);
      attachDrag(e.target.shadowRoot);
    }
  }
});