Last active
May 8, 2022 16:20
-
-
Save Firsh/d93489879821c25ef049082d99c076c1 to your computer and use it in GitHub Desktop.
Userscript to open links to PDF files on a new tab
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 PDF new tab | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://*/* | |
// @match http://*/* | |
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
document.querySelectorAll('a[href*=".pdf"]').forEach(el => { | |
el.setAttribute('target','_blank'); | |
var expression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi; | |
var regex = new RegExp(expression); | |
var t = el.getAttribute('href'); | |
var match = expression.exec(t); | |
if (match[0]) { | |
el.setAttribute('href', match[0]); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment