// ==UserScript==
// @name Handlers Helper
// @namespace Violentmonkey Scripts
// @match http*://*/*
// @grant none
// @version 1.1
// @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)
if (attr.startsWith('http')) {
url = attr;
}
if (url == '') {
var 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;}
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');
}
//}
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);
}
}
});