Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active April 24, 2022 16:16
Show Gist options
  • Save Utopiah/6ec7169c37cdd07ba6003e3004741d6d to your computer and use it in GitHub Desktop.
Save Utopiah/6ec7169c37cdd07ba6003e3004741d6d to your computer and use it in GitHub Desktop.
// to be used with a shortcut to get a DOI, e.g on Tridactyl on a Frontiers page :
// bind q js var ref = document.querySelector('div.References:hover'); var doi; if (ref) doi = ref.innerText.replace(/.*doi:/, "").split("\n")[0]; if (doi) fetch('http://localhost:3467/?doi='+doi).then(response => response.json()).then(data => alert(data.result));
/* to visually highlight what references have already been sent :
var ref = document.querySelector('div.References:hover');
if (ref) ref.innerText.replace(/.*doi/, "doi").split("\n")[0];
var text = ref.firstChild.lastChild.textContent.split("doi:");
ref.firstChild.lastChild.textContent = text[0];
var el = document.createElement("span");
el.style.background = "lightblue";
el.textContent = "doi:" + text[1];
ref.firstChild.appendChild(el);
*/
const fs = require('fs');
const express = require('express')
const cors = require('cors')
const https = require('https')
const path = require('path')
const {execSync} = require('child_process');
const app = express()
app.use(cors())
// could check for protonvpn-cli s first and if disabled do protonvpn-cli r
const URL="https://legitimate-portal.net";
app.get('/', function(req, res){
console.log(req.query.doi);
var DOI=req.query.doi;
if (!DOI) res.json({"status":"DOI missing"})
var filename = DOI.replaceAll("\/","_").replaceAll(".","-")+".pdf";
var getPage = `curl ${URL}/${DOI} | grep "button onclick" | sed "s/.*='//" | sed "s/'.*//"`
// could replace curl and parsing with fetch and proper JS instead
var page = execSync(getPage, {cwd:"pdfs"}).toString().replaceAll("\n","");
if (!page) {
res.json({"result":"no document found"});
console.log("failed, no result found");
return
}
var target = URL+page
if (page.indexOf("//") > -1 ) target = "https:" + page
// might return a full URL instead, if so then don't prepend with the hardcoded domain
var getPDFcmd = `wget ${target} -O ${filename}`
var newpdf = execSync(getPDFcmd, {cwd:"pdfs"}).toString()
var cmd = `scp ${filename} remarkable2:/home/root/ && ssh remarkable2 -t "source /home/root/.bashrc; addWithMetadataIfNew ${filename}; systemctl restart xochitl"`
// assumes all ssh goes well. Should at least check if the device is reachable first.
var sendResultToLocalRemarkable = execSync(cmd, {cwd:"pdfs"})
res.json({"status":"test", "filename":filename, "result":filename})
});
app.use('/static', express.static(path.join(__dirname, 'pdfs')))
// accessible only directly e.g. http://localhost:3467/static/modifiedDOI.pdf
// could instead return a JSON of available files
const port = 3467
app.listen(port, () =>
console.log('Remarkable file send via DOI listening on port', port)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment