Created
April 6, 2023 16:35
-
-
Save stefanschmidt/d13a813224167c48aae67d1c2a10dd28 to your computer and use it in GitHub Desktop.
Restore cmd+click to open an article in a new tab on MIT Technology Review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Anti-Clickjacking | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description restore cmd+click on article links to open new tab | |
// @author Stefan | |
// @match https://www.technologyreview.com/ | |
// @icon https://www.technologyreview.com/static/media/favicon.1cfcdb44.ico | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let link_selector = 'div > div[class^="homepageStoryCard__cardContent"] > a'; | |
let delay = 500; // recommendation for Chrome, not needed for Safari | |
setTimeout(function(){ | |
function removeListenersFromAnchors() { | |
let feedNodeList = document.querySelectorAll(link_selector); | |
for (let i = 0; i < feedNodeList.length; i++) { | |
let feedNode = feedNodeList[i]; | |
// temporarily remove DOMNodeInserted listener to avoid RangeError: Maximum call stack size exceeded | |
document.removeEventListener('DOMNodeInserted', removeListenersFromAnchors, false); | |
// approach from: https://stackoverflow.com/questions/9251837 | |
let fixedFeedNode = feedNode.cloneNode(true); | |
feedNode.parentNode.replaceChild(fixedFeedNode, feedNode); | |
document.addEventListener('DOMNodeInserted', removeListenersFromAnchors, false); | |
} | |
} | |
document.addEventListener('DOMNodeInserted', removeListenersFromAnchors, false); | |
}, delay); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment