Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
paper-man/paper-man.user.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
173 lines (165 sloc)
7.62 KB
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 Paper-Man | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Download buttons for APS journals, springer link, nature and sciencedirect. | |
// @author Philipp Bredol | |
// @match https://*/* | |
// @grant GM_download | |
// @grant GM_openInTab | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant window.close | |
// ==/UserScript== | |
(async function() { | |
'use strict'; | |
async function readArray(name) { | |
var arr = undefined | |
try { | |
arr = JSON.parse(await GM_getValue(name, '')) | |
} catch (e) { | |
return [] | |
} | |
if (!Array.isArray(arr)) { | |
return [] | |
} | |
return arr | |
} | |
async function writeArray(name, arr) { | |
await GM_setValue(name, JSON.stringify(arr)) | |
} | |
if(location.href.startsWith('https://journals.aps.org')) { | |
var div = document.getElementById('export-article-link').parentNode | |
var as = div.getElementsByTagName('a') | |
var aPdf = as[0] | |
var aBib = as[1] | |
var aDown = document.createElement('a') | |
aDown.className = 'small button' | |
aDown.innerHTML = 'Download pdf and citation' | |
aDown.addEventListener('click', (e) => { | |
var loc = location.href.split('/') | |
var loc2 = loc.pop() | |
var loc1 = loc.pop() | |
var dataId = loc1+'/'+loc2 | |
var baseUrl = loc[0]+'/'+loc[1]+'/'+loc[2]+'/'+loc[3] | |
GM_download({'url': baseUrl+'/export/'+dataId, 'name': 'aps-paper.bib', | |
'onerror': (err)=>{alert(JSON.stringify(err))}}) | |
GM_download({'url': baseUrl+'/pdf/'+dataId, 'name': 'aps-paper.pdf', | |
'onerror': (err)=>{alert(JSON.stringify(err))}}) | |
}) | |
div.insertBefore(aDown, null) | |
} | |
else if(location.href.startsWith('https://link.springer.com')) { | |
var div = document.getElementsByClassName('download-article test-pdf-link')[0] | |
if(div == undefined) { | |
div = document.getElementsByClassName('test-download-book-options u-interface download-book-link')[0] | |
} | |
var newDiv = document.createElement('div') | |
newDiv.className = 'c-button' | |
newDiv.style = 'margin-top:15px' | |
newDiv.innerHTML = 'Download pdf and citation' | |
newDiv.addEventListener('click', ()=>{ | |
var loc = location.href.split('/') | |
var loc2 = loc.pop() | |
var loc1 = loc.pop() | |
var dataId = loc1+'/'+loc2 | |
GM_download({'url': 'https://citation-needed.springer.com/v2/references/'+dataId+'?format=bibtex&flavour=citation', | |
'name': 'springer-paper.bib', 'onerror': (err)=>{alert(err)}}) | |
GM_download({'url': 'https://link.springer.com/content/pdf/'+dataId+'.pdf', | |
'name': 'springer-paper.pdf', 'onerror': (err)=>{alert(err)}}) | |
}) | |
div.parentNode.insertBefore(newDiv, div.nextSibling) | |
} | |
else if(location.href.startsWith('https://www.nature.com')) { | |
function downloadNaturePaper(articleUrl) { | |
GM_download({'url': articleUrl+'.ris', 'name': 'nature-paper.ris', 'onerror': (err)=>{alert(JSON.stringify(err))}}) | |
GM_download({'url': articleUrl+'.pdf', 'name': 'nature-paper.pdf', 'onerror': (err)=>{alert(JSON.stringify(err))}}) | |
} | |
// Add paper download button in 'single article view' | |
var div = document.getElementsByClassName('c-pdf-download u-clear-both')[0] | |
if (div) { | |
var newDiv = document.createElement('div') | |
newDiv.className = 'c-pdf-download__link' | |
newDiv.style = 'margin-top:-15px;margin-bottom:15px;cursor:pointer' | |
newDiv.innerHTML = 'Download pdf and citation' | |
newDiv.addEventListener('click', ()=>{downloadNaturePaper(location.href)}) | |
div.parentNode.insertBefore(newDiv, div.nextSibling) | |
} | |
// Add checkboxes and coloring for 'article overview' | |
else { | |
window.setTimeout(()=>{location.reload()}, (10+Math.random())/10 * 3*60*60*1e3) | |
var arts = document.getElementsByTagName('article') | |
var readArticles = await readArray('read-nature-articles') | |
var seenArticles = await readArray('seen-nature-articles') | |
for (var i=0; i<arts.length; i++) { | |
var art = arts[i] | |
var a = art.getElementsByTagName('a')[0] | |
var href = a.getAttribute('href') | |
if (!href.startsWith('/')) { | |
continue | |
} | |
var paperUrl = 'https://www.nature.com'+href | |
var cb = document.createElement('input') | |
cb.type = 'checkbox' | |
if (readArticles.indexOf(paperUrl) > 0) { | |
art.style.backgroundColor = '#ddffdd' | |
cb.checked = true | |
cb.disabled = true | |
} | |
else { | |
if (seenArticles.indexOf(paperUrl) > 0) { | |
art.style.backgroundColor = '#ffffee' | |
} | |
else { | |
art.style.backgroundColor = '#eeeeff' | |
if (!document.title.startsWith('*')) { | |
document.title = '*'+document.title | |
} | |
art.addEventListener('mouseover', (async (cb, art, a, paperUrl)=>{ | |
art.style.backgroundColor = '#ffffee' | |
var seen = await readArray('seen-nature-articles') | |
seen.push(paperUrl) | |
await writeArray('seen-nature-articles', seen) | |
}).bind(0, cb, art, a, paperUrl)) | |
} | |
cb.addEventListener('click', (async (cb, art, a, paperUrl)=>{ | |
cb.checked = true | |
cb.disabled = true | |
art.style.backgroundColor = '#ddffdd' | |
downloadNaturePaper(paperUrl) | |
var read = await readArray('read-nature-articles') | |
read.push(paperUrl) | |
await writeArray('read-nature-articles', read) | |
}).bind(0, cb, art, a, paperUrl)) | |
} | |
a.parentNode.insertBefore(cb, a) | |
} | |
} | |
} | |
else if(location.href.startsWith('https://www.sciencedirect.com')) { | |
var div = document.getElementById('export-citation') | |
var newDiv = document.createElement('div') | |
newDiv.id = 'tamperedNode' | |
newDiv.className = 'ExportCitation' | |
newDiv.style = 'cursor:pointer' | |
newDiv.innerHTML = '<a>Download pdf and citation</a>' | |
newDiv.addEventListener('click', ()=>{ | |
GM_openInTab(location.href+'/pdf') | |
GM_download({'url': 'https://www.sciencedirect.com/sdfe/arp/cite?format=text/x-bibtex&withabstract=true&pii=' | |
+location.href.split('/').pop(), | |
'name': 'sciencedirect-paper.bib', | |
'onerror': (err)=>{alert(JSON.stringify(err))}}) | |
}) | |
window.setInterval(() => { | |
if(!document.getElementById('tamperedNode')) { | |
div.parentNode.insertBefore(newDiv, div.nextSibling) | |
} | |
}, 200) | |
} | |
else if(location.href.startsWith('https://pdf.sciencedirectassets.com')) { | |
GM_download({'url': location.href, | |
'name': 'sciencedirect-paper.pdf', | |
'onerror': (err) => {alert(JSON.stringify(err))}, | |
'onload': window.close }) | |
} | |
})(); |