Skip to content

Instantly share code, notes, and snippets.

@ms609
Last active November 24, 2023 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms609/beaed274ec473fbe0f929d1ea88e4158 to your computer and use it in GitHub Desktop.
Save ms609/beaed274ec473fbe0f929d1ea88e4158 to your computer and use it in GitHub Desktop.
Scientific article PDF redirection

To use:

  1. Install the Tampermonkey (or GreaseMonkey) browser plugin
  2. Open the browser plugin and use the "+" button to add a script
  3. Copy & paste the contents of wiley_pdf.js and elsevier.js to separate scripts
  4. On the "settings" tab, enter the "Raw" URL in the "Updates→Update URL" field

For a more aggressive solution, see the Publication Auto-PDF scripts.

// ==UserScript==
// @name CUP PDF linker
// @namespace https://gist.github.com/ms609/beaed274ec473fbe0f929d1ea88e4158
// @version 0.1
// @description Go directly to the PDF
// @author Martin R. Smith
// @run-at document-end
// @match https://www.cambridge.org/core/journals/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=cambridge.org
// @grant none
// ==/UserScript==
// Status: Link is created but disappears after 1s or so. Can't work out how to make it stick...
(function() {
'use strict';
function AddLink () {
let id = document.location.href.match(/[\dA-Z]{20,}/)[0];
var btn = document.getElementById("save-pdf-dropdown");
let pdfURL = "https://www.cambridge.org/core/services/aop-cambridge-core/content/view/" + id;
var newA = document.createElement("a");
newA.href = pdfURL;
newA.id = "added-link";
newA.innerHTML = "PDF (direct link)";
let position = document.getElementById("save-pdf-dropdown");
position.parentElement.insertBefore(newA, position);
console.log(id);
}
if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {
AddLink();
} else {
document.addEventListener("DOMContentLoaded", function(event) {
AddLink();
});
}
})();
// ==UserScript==
// @name ScienceDirect pdf
// @namespace https://gist.github.com/ms609/beaed274ec473fbe0f929d1ea88e4158
// @version 0.1
// @description Go directly to the Elsevier PDF, not the "epdf"
// @author Martin R. Smith
// @match https://www.sciencedirect.com/*
// @match https://reader.elsevier.com/reader/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=sciencedirect.com
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
function PDFURL (id) {
return "https://www.sciencedirect.com/science/article/pii/" + id + "/pdfft";
}
function ReplaceHrefs() {
var anchors = document.getElementsByTagName("a");
for (var i = 0, len = anchors.length; i < len; i++) {
if (anchors[i].getAttribute("role") == "button") {
console.log(anchors[i])
console.log(anchors[i].href)
}
if (anchors[i].href != "") {
var match = anchors[i].href.match(/\/science\/article\/pii\/(S\d+X?)\/pdfft/);
if (match) {
anchors[i].href = PDFURL(match[1]);
}
} else {
// Doesn't quite work - suggestions welcome!
anchors[i].href = PDFURL(document.location.href.match(/S\d+X?/)[0]);
var newA = document.createElement("a");
newA.href = PDFURL(document.location.href.match(/S\d+/)[0]);
newA.innerHTML = "PDF (direct)";
document.insertBefore(newA, anchors[i]);
}
}
}
// https://www.sciencedirect.com/sdfe/reader/pii/S0031018221004983/pdf
let inURL = document.location.href.match(/https:\/\/reader\.elsevier\.com\/reader\/sd\/pii\/(S[\dX]+)/);
if (inURL) {
document.location.href = PDFURL(inURL[1]);
}
if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {
ReplaceHrefs();
} else {
document.addEventListener("DOMContentLoaded", function(event) {
ReplaceHrefs();
});
}
})();
// ==UserScript==
// @name epdf -> pdf
// @namespace https://gist.github.com/ms609/beaed274ec473fbe0f929d1ea88e4158
// @version 0.1.1
// @description Go directly to the PDF, not the "epdf"
// @author Martin R. Smith
// @match https://aslopubs.onlinelibrary.wiley.com/*
// @match https://www.journals.uchicago.edu/doi/*
// @match https://www.lyellcollection.org/doi/*
// @match https://onlinelibrary.wiley.com/*
// @match https://www.idunn.no/doi/*
// @match https://pnas.org/doi/*
// @match https://www.pnas.org/doi/*
// @match https://www-pnas-org.ezphost.dur.ac.uk/doi/*
// @match https://royalsocietypublishing.org/doi/*
// @match https://www.science.org/doi/*
// @match https://www.tandfonline.com/doi/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=wiley.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
if (document.location.href.match("/epdf/")) {
document.location.href = document.location.href.replace("/epdf/", "/pdf/");
} else {
var anchors = document.getElementsByTagName("a");
for (var i = 0, len = anchors.length; i < len; i++) {
anchors[i].href = anchors[i].href.replace("/epdf/", "/pdf/");
}
}
})();
// ==UserScript==
// @name Sci-hub frame remover
// @namespace https://gist.github.com/ms609/beaed274ec473fbe0f929d1ea88e4158
// @version 0.1
// @description Go straight to PDF
// @author Martin R. Smith
// @match https://sci-hub.yncjkj.com/10.*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @run-at document-idle
// ==/UserScript==
function MaximizeFrame () {
let pdfFrame = document.getElementById("pdf");
if (pdfFrame) {
document.location.href = pdfFrame.src.replace(/#.*$/, "");
} else {
window.addEventListener("load", MaximizeFrame, false);
}
}
(function() {
"use strict";
MaximizeFrame();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment