Greasy Fork is available in English.

Discuții » Dezvoltare

Simple development question, help (How to open hovered link on mouse right+down click action?)

§
Postat în: 07-01-2021
Editat în: 07-01-2021

I'm trying to make the script open any hovered link when I right click and hold the mouse down on the link element.
The script works now, but not for the element link I selected, it works only once for any random hovered link that I hover after my mouse action


// ==UserScript==
// @name Mouse Gestures
// @version 0.1
// @include *
// @run-at document-start
// @grant GM_openInTab
// @grant window.close
// ==/UserScript==

// --- Settings ---

const SENSITIVITY = 3; // 1 ~ 5
const TOLERANCE = 3; // 1 ~ 5

const funcs = {
'D': function() { //THIS IS THE IMPORTANT PART
document.addEventListener('mouseover', listener, false); //THIS IS THE IMPORTANT PART
} //THIS IS THE IMPORTANT PART
};

var listener = function(event) { //THIS IS THE IMPORTANT PART
var hoveredEl = event.target; // The actual element which was hovered. //THIS IS THE IMPORTANT PART
if (hoveredEl.tagName !== 'A') { return; } // Ignore non links //THIS IS THE IMPORTANT PART
window.open(hoveredEl.href); //THIS IS THE IMPORTANT PART
document.removeEventListener('mouseover', listener, false); //THIS IS THE IMPORTANT PART
}; //THIS IS THE IMPORTANT PART

//NOT IMPORTANT PART BELOW
// ---------------------------------------------------------------

const s = 1 << ((7 - SENSITIVITY) << 1);
const t1 = Math.tan(0.15708 * TOLERANCE),
t2 = 1 / t1;

let x, y, path;

const tracer = function(e) {
let cx = e.clientX,
cy = e.clientY,
deltaX = cx - x,
deltaY = cy - y,
distance = deltaX * deltaX + deltaY * deltaY;
if (distance > s) {
let slope = Math.abs(deltaY / deltaX),
direction = '';
if (slope > t1) {
direction = deltaY > 0 ? 'D' : 'U';
} else if (slope <= t2) {
direction = deltaX > 0 ? 'R' : 'L';
}
if (path.charAt(path.length - 1) !== direction) {
path += direction;
}
x = cx;
y = cy;
}
};

window.addEventListener('mousedown', function(e) {
if (e.which === 3) {
x = e.clientX;
y = e.clientY;
path = "";
window.addEventListener('mousemove', tracer, false);
}
}, false);

window.addEventListener('contextmenu', function(e) {
window.removeEventListener('mousemove', tracer, false);
if (path !== "") {
e.preventDefault();
if (funcs.hasOwnProperty(path)) {
funcs[path]();
}
}
}, false);

§
Postat în: 07-01-2021
Editat în: 07-01-2021

Problem solved

// ==UserScript==
// @name ouse Gestures
// @version 0.1
// @include *
// @run-at document-start
// @grant GM_openInTab
// @grant window.close
// ==/UserScript==
var link
document.addEventListener('mouseover', function(event) {
var hoveredEl = event.target; // The actual element which was hovered.
if (hoveredEl.tagName !== 'A') { return; } // Ignore non links
link = hoveredEl.href;
});

// --- Settings ---

const SENSITIVITY = 3; // 1 ~ 5
const TOLERANCE = 3; // 1 ~ 5

const funcs = {
'DR': function() {
window.top.close();
},
//'U': function() {
//window.scrollTo(0, 0);
//},
'D': function() {
window.open(link);
},
//'L': function() {
//window.history.back();
//},
//'R': function() {
//window.history.forward();
//},
//'RU': function() {
//GM_openInTab('about:newtab', false);
//},
//'UD': function() {
//window.location.reload();
//}
};

// ----------------

const s = 1 << ((7 - SENSITIVITY) << 1);
const t1 = Math.tan(0.15708 * TOLERANCE),
t2 = 1 / t1;

let x, y, path;

const tracer = function(e) {
let cx = e.clientX,
cy = e.clientY,
deltaX = cx - x,
deltaY = cy - y,
distance = deltaX * deltaX + deltaY * deltaY;
if (distance > s) {
let slope = Math.abs(deltaY / deltaX),
direction = '';
if (slope > t1) {
direction = deltaY > 0 ? 'D' : 'U';
} else if (slope <= t2) {
direction = deltaX > 0 ? 'R' : 'L';
}
if (path.charAt(path.length - 1) !== direction) {
path += direction;
}
x = cx;
y = cy;
}
};

window.addEventListener('mousedown', function(e) {
if (e.which === 3) {
x = e.clientX;
y = e.clientY;
path = "";
window.addEventListener('mousemove', tracer, false);
}
}, false);

window.addEventListener('contextmenu', function(e) {
window.removeEventListener('mousemove', tracer, false);
if (path !== "") {
e.preventDefault();
if (funcs.hasOwnProperty(path)) {
funcs[path]();
}
}
}, false);

Postează un raspuns

Autentifică-te pentru a posta un răspuns.