Last active
November 13, 2020 09:41
-
-
Save jeeger/a5c2f91f8ab0561394a35f4ecfe649e9 to your computer and use it in GitHub Desktop.
Revisions
-
jeeger revised this gist
Nov 13, 2020 . 1 changed file with 70 additions and 37 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,50 +1,83 @@ // ==UserScript== // @name AlternativeDoiResolver // @version 1 // @grant GM.xmlHttpRequest // @description Converts the pretty useless DOI links to clickable links ot a custom resolver. // @include *sciencedirect* // @include *springer* // @include *ieeexplore* // @include *acm.org* // @require https://code.jquery.com/jquery-3.5.1.min.js // ==/UserScript== jQuery('head').append('<style type="text/css">a.doilink { background-color: rgba(245, 132, 66, 0.5); border-radius: 0.5em; padding: 0.3em; margin: 0.8em; font-weight: bold; display: block; }</style>'); function getUrl() { GM.xmlHttpRequest({ method: "GET", url: "https://sci-hub.now.sh", onload: function(response) { console.debug("Got response from sci-hub dispatcher."); var urls = $(response.responseText).find(".biglink").map((index, elem) => new URL($(elem).attr('href'))); do_resolve(urls); }, onerror: function(errsponse) { console.error("Failed to access sci-hub dispatcher, error ${errsponse.statusText}. Using default value."); do_resolve([new URL("http://sci-hub.tw/")]); } }) } function do_resolve(resolver_urls) { function updateLink() { // Using XPath // Allows us to get span or a results. var result = document.evaluate('//*[(self::span or self::a) and (starts-with(text(),"https://doi") or starts-with(@href, "https://doi"))]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); console.debug("Found " + result.snapshotLength + " results."); if (result.snapshotLength == 0) { console.warn("No links found, retrying in a second."); setTimeout(updateLink, 1000); return; } for (var i = 0; i < result.snapshotLength; i++) { var node = result.snapshotItem(i); var newlink = document.createElement("a"); var url; if (node.tagName == "A") { // Take href of node url = new URL(node.href); } else if (node.tagName == "SPAN") { url = new URL(node.innerHTML); } else { console.error("Unknown element type " + node.tagName); return; }; console.log(resolver_urls); var fragment = jQuery('<p></p>').insertAfter(node); $(resolver_urls).each((index, resolver_url) => { console.log(resolver_url); var final_url = new URL(resolver_url); if (final_url.pathname.endsWith('/')) { console.log("Final url pathname: " + final_url.pathname); final_url.pathname = final_url.pathname.slice(0, -1) + url.pathname; } else { final_url.pathname = final_url.pathname + url.pathname; } jQuery('<a href="' + final_url.href + '" class="doilink">' + resolver_url + '</a>').appendTo(fragment); }); fragment.hide().insertAfter(node).fadeIn(1000); }; } console.log("Setting update timeout."); setTimeout(updateLink, 1000); } jQuery(getUrl); -
jeeger revised this gist
May 22, 2018 . 1 changed file with 11 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,12 @@ // ==/UserScript== // Modify if the resolver URL changes again. var resolver_url = "https://resolver.tw/"; var css = 'a.faded_out { opacity: 0; transition: none; } a.faded_in {opacity: 1; transition: 2s opacity;}'; var style = document.createElement('style'); style.type = 'text/css'; style.appendChild(document.createTextNode(css)); document.head.appendChild(style); function updateLink() { // Using XPath @@ -35,8 +40,11 @@ function updateLink() { } newlink.href = resolver_url + "https://doi.org" + url.pathname; newlink.innerHTML = "Resolve it!"; newlink.className = 'faded_out'; newlink.style = "margin: 0.8em; font-weight: bold;"; node.parentNode.insertBefore(newlink, node.nextSibling); setTimeout(function() {document.querySelector('a.faded_out').className = 'faded_in';}, 500); } } setTimeout(updateLink, 5000); -
jeeger revised this gist
Mar 1, 2018 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,15 +5,20 @@ // @description Converts the pretty useless DOI links to clickable links ot a custom resolver. // @include *sciencedirect* // @include *springer* // @include *ieeexplore* // ==/UserScript== // Modify if the resolver URL changes again. var resolver_url = "resolver.la"; function updateLink() { // Using XPath // Allows us to get span or a results. var result = document.evaluate('//*[(self::span or self::a) and (starts-with(text(),"https://doi") or starts-with(@href, "https://doi"))]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); console.log("Found " + result.snapshotLength + " results."); for (var i = 0; i < result.snapshotLength; i++) { var node = result.snapshotItem(i); -
jeeger created this gist
Feb 28, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ // ==UserScript== // @name AlternativeDoiResolver // @version 1 // @grant none // @description Converts the pretty useless DOI links to clickable links ot a custom resolver. // @include *sciencedirect* // @include *springer* // ==/UserScript== // Modify if the resolver URL changes again. var resolver_url = "your_resolver_here.la"; function updateLink() { // Using XPath // Allows us to get span or a results. var result = document.evaluate('//*[(self::span or self::a) and starts-with(text(),"https://doi")]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null) console.log("Found " + result.snapshotLength + " results."); for (var i = 0; i < result.snapshotLength; i++) { var node = result.snapshotItem(i); var newlink = document.createElement("a"); var url; if (node.tagName == "A") { // Take href of node url = new URL(node.href); } else if (node.tagName == "SPAN") { url = new URL(node.innerHTML); } else { console.log("Unknown element type " + node.tagName); return; } newlink.href = resolver_url + "https://doi.org" + url.pathname; newlink.innerHTML = "Resolve it!"; node.parentNode.insertBefore(newlink, node); } } setTimeout(updateLink, 10000);