Skip to content

Instantly share code, notes, and snippets.

@jeeger
Last active November 13, 2020 09:41
Show Gist options
  • Save jeeger/a5c2f91f8ab0561394a35f4ecfe649e9 to your computer and use it in GitHub Desktop.
Save jeeger/a5c2f91f8ab0561394a35f4ecfe649e9 to your computer and use it in GitHub Desktop.
Custom DOI resolver (tested for Springerlink and ScienceDirect), compatible with TUM eaccess proxy.
// ==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
Copy link
Author

jeeger commented Mar 1, 2018

Added ieeexplore functionality.

@jeeger
Copy link
Author

jeeger commented May 22, 2018

Added animation.

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