I’m trying to get a simple Chrome plugin to skip forward and backwards in youtube when the mousewheel is scrolled vertically.
That is working fine, but unfortunately the page also scrolls up and down when I use the mousehweel. I am trying to cancel the propagation of the mousewheel event with no success.
I have tried to no avail:
e.preventDefault();
return false;
e.stopImmediatePropagation();
Here is the code, as you can see I’ve tried many things.
There are only two JS files:
const playerdata = {
"www.youtube.com" : {
"keyCodes" : [39, 37],
//"keyCodes" : [76, 74],
"elem" : document
}
}
var curPlayer = playerdata[location.host]
function triggerKeydown(forward) {
//try {
var elem;
var e = new Event('keydown');
e.keyCode = forward ? curPlayer.keyCodes[0] : curPlayer.keyCodes[1];
if (typeof curPlayer.elem == "string") {
elem = document.querySelector(curPlayer.elem)
} else {
elem = curPlayer.elem
}
//$(window).scrollTop(); // error on $
e.preventDefault();
elem.dispatchEvent(e);
//e.preventDefault();
//e.stopImmediatePropagation();
//} catch(ex) {
//alert(ex); // no error
//}
//e.preventDefault();
//e.stopImmediatePropagation();
//return false;
}
document.addEventListener("wheel", function(e) {
//e.preventDefault();
if (e.deltaX == 0 && e.deltaZ == 0) {
if (e.deltaY > 0) {
//e.preventDefault();
triggerKeydown(false);
} else if (e.deltaY < 0) {
//e.preventDefault();
triggerKeydown(true);
}
}
});
function injectScript(file_path, tag) {
var node = document.getElementsByTagName(tag)[0];
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', file_path);
node.appendChild(script);
}
injectScript(chrome.extension.getURL('video-seek-mousewheel.js'), 'body');