Skip to content

Instantly share code, notes, and snippets.

@heavyLobster2
Last active December 12, 2018 08:29
Show Gist options
  • Save heavyLobster2/b2545b34941ed891f4d19390bc0a6e7b to your computer and use it in GitHub Desktop.
Save heavyLobster2/b2545b34941ed891f4d19390bc0a6e7b to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Prevent Browser Hijacking
// @description Hit Ctrl+Shift+F12 to stop sites from breaking standard browser functionality like copy and paste
// @version 1.1.1
// @author heavyLobster2
// @namespace github.com/heavyLobster2
// @downloadURL https://gist.github.com/heavyLobster2/b2545b34941ed891f4d19390bc0a6e7b/raw/PreventBrowserHijacking.user.js
// @match *://*/*
// @run-at document-start
// @grant none
// ==/UserScript==
(function () {
"use strict";
var all = false;
function toggleAll() {
all = !all;
}
function stop(event) {
event.stopImmediatePropagation();
}
function stopIfAll(event) {
if (all) stop(event);
}
var listeners = new Map([
["keydown", function (e) {
if (e.code === "F12") { //F12
if (e.ctrlKey && e.altKey && e.shiftKey) { //Ctrl+Alt+Shift+F12
suicide();
} else if (e.ctrlKey && e.shiftKey) { //Ctrl+Shift+F12
toggleAll();
} else if (e.ctrlKey) { //Ctrl+F12
debugger;
}
stop(e);
} else if (e.ctrlKey && e.shiftKey && e.code === "KeyI") { //Ctrl+Shift+I
stop(e);
}
}],
["click", function (e) {
if (e.button === 1) { //middle-click
stop(e);
} else {
stopIfAll(e);
}
}],
["mousedown", function (e) {
stopIfAll(e);
}],
["cut", function (e) {
stopIfAll(e);
}],
["copy", function (e) {
stopIfAll(e);
}],
["paste", function (e) {
stopIfAll(e);
}],
["contextmenu", function (e) {
stopIfAll(e);
}],
["drag", function (e) {
stopIfAll(e);
}],
["drop", function (e) {
stopIfAll(e);
}]
]);
listeners.forEach(function (value, key) {
window.addEventListener(key, value, true);
});
function suicide() {
listeners.forEach(function (value, key, map) {
window.removeEventListener(key, value, true);
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment