Skip to content

Instantly share code, notes, and snippets.

@kobachi
Last active March 19, 2022 10:50
Show Gist options
  • Save kobachi/bbdc0f26f23a46c01da8d344d921ed8b to your computer and use it in GitHub Desktop.
Save kobachi/bbdc0f26f23a46c01da8d344d921ed8b to your computer and use it in GitHub Desktop.
Selection Disabler Canceller
// ==UserScript==
// @name Selection Disabler Canceller
// @version 1.1
// @include http://*
// @include https://*
// @grant none
// ==/UserScript==
(() => {
const GM_log = () => {};//unsafeWindow.console.log;
const REMOVE_STYLE = ["-webkit-touch-callout", "-webkit-user-select", "-khtml-user-select", "-moz-user-select", "-ms-user-select", "user-select"];
const removeStyle = function(rule) {
var removed = false;
REMOVE_STYLE.forEach(name => {
if (typeof rule.style[name] != "undefined" && rule.style[name] != "") {
rule.style[name] = "";
removed = true;
}
});
return removed;
};
const cleanDisabler = function(tag) {
var clean = false;
if (document.body.onselectstart != null) {
document.body.onselectstart = null;
clean = true;
GM_log("[on" + tag + "] onSelectStart event removed: document.body");
}
const disabledElements = Array.from(document.querySelectorAll("*[onselectstart]"));
for (var i = 0; i < disabledElements.length; i++) {
try {
if (!disabledElements[i].dispatchEvent(new Event("selectstart"))) {
disabledElements[i].onselectstart = null;
clean = true;
GM_log("[on" + tag + "] onSelectStart event removed: " + disabledElements[i].tagName);
}
} catch (e) {
disabledElements[i].onselectstart = null;
clean = true;
GM_log("[on" + tag + "] onSelectStart event removed*: " + disabledElements[i].tagName);
}
}
const removedRules = Array.from(document.styleSheets)
.flatMap(s => {
try {
return Array.from(s.rules);
} catch (e) {
return null;
}
})
.filter(r => r != null && typeof r.style != "undefined")
.filter(r => removeStyle(r));
if (0 < removedRules.length) {
removedRules.forEach(rule => {
GM_log("[on" + tag + "] user-select style removed: " + rule.selectorText);
});
clean = true;
}
if (clean) {
GM_log("[on" + tag + "] Selection disabler cleanup successfuly finished.");
} else {
GM_log("[on" + tag + "] Selection disabler cleanup not applied.");
}
};
window.addEventListener("load", () => { cleanDisabler("Load") });
cleanDisabler("Init");
})();
@kobachi
Copy link
Author

kobachi commented Mar 19, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment