// ==UserScript==
// @name Download arxiv pdf and rename it with title
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 下载并用文章标题命名arxiv的pdf,支持arxiv.org/pdf/xx.pdf 和 cn.arxiv.org/pdf/xx.pdf两种页面,按 Ctrl+S 触发下载。Automatially download an opened arxiv pdf file and rename it with its title. Work on pages like arxiv.org/pdf/xx.pdf or cn.arxiv.org/pdf/xx.pdf. Press Ctrl+S to trigger.
// @author LTGuo
// @include http://cn.arxiv.org/pdf/*
// @include https://arxiv.org/pdf/*
// @grant none
// ==/UserScript==
/*! hotkeys-js v3.3.5 | MIT (c) 2018 kenny wong <[email protected]> | http://jaywcjlove.github.io/hotkeys */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function f(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on"+t,function(){n(window.event)})}function l(e,t){for(var n=t.slice(0,t.length-1),o=0;o<n.length;o++)n[o]=e[n[o].toLowerCase()];return n}function s(e){e||(e="");for(var t=(e=e.replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}function d(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,r=!0,i=0;i<n.length;i++)-1===o.indexOf(n[i])&&(r=!1);return r}for(var t={backspace:8,tab:9,clear:12,enter:13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},p={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":e?224:91,cmd:e?224:91,command:e?224:91},u=[],h={16:"shiftKey",18:"altKey",17:"ctrlKey"},v={16:!1,18:!1,17:!1},g={},n=1;n<20;n++)t["f"+n]=111+n;var o="all",y=v[e?224:91]=!(h[e?224:91]="metaKey"),w=function(e){return t[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){o=e||"all"}function m(){return o||"all"}function O(e,t,n){var o=void 0;if(t.scope===n||"all"===t.scope){for(var r in o=0<t.mods.length,v)Object.prototype.hasOwnProperty.call(v,r)&&(!v[r]&&-1<t.mods.indexOf(+r)||v[r]&&-1===t.mods.indexOf(+r))&&(o=!1);(0!==t.mods.length||v[16]||v[18]||v[17]||v[91])&&!o&&"*"!==t.shortcut||!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function b(e,t,n){var o=s(e),r=[],i="all",a=document,c=0;for(void 0===n&&"function"==typeof t&&(n=t),"[object Object]"===Object.prototype.toString.call(t)&&(t.scope&&(i=t.scope),t.element&&(a=t.element)),"string"==typeof t&&(i=t);c<o.length;c++)r=[],1<(e=o[c].split("+")).length&&(r=l(p,e)),(e="*"===(e=e[e.length-1])?"*":w(e))in g||(g[e]=[]),g[e].push({scope:i,mods:r,shortcut:o[c],method:n,key:o[c]});void 0===a||y||(y=!0,f(a,"keydown",function(e){!function(e){var t=g["*"],n=e.keyCode||e.which||e.charCode;if(-1===u.indexOf(n)&&u.push(n),93!==n&&224!==n||(n=91),n in v){for(var o in v[n]=!0,p)p[o]===n&&(b[o]=!0);if(!t)return}for(var r in v)Object.prototype.hasOwnProperty.call(v,r)&&(v[r]=e[h[r]]);if(b.filter.call(this,e)){var i=m();if(t)for(var a=0;a<t.length;a++)t[a].scope===i&&O(e,t[a],i);if(n in g)for(var c=0;c<g[n].length;c++)O(e,g[n][c],i)}}(e)}),f(a,"keyup",function(e){!function(e){var t=e.keyCode||e.which||e.charCode,n=u.indexOf(t);if(n<0||u.splice(n,1),93!==t&&224!==t||(t=91),t in v)for(var o in v[t]=!1,p)p[o]===t&&(b[o]=!1)}(e)}))}var r={setScope:i,getScope:m,deleteScope:function(e,t){var n=void 0,o=void 0;for(var r in e||(e=m()),g)if(Object.prototype.hasOwnProperty.call(g,r))for(n=g[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;m()===e&&i(t||"all")},getPressedKeyCodes:function(){return u.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=w(e)),-1!==u.indexOf(e)},filter:function(e){var t=e.target.tagName||e.srcElement.tagName;return!("INPUT"===t||"SELECT"===t||"TEXTAREA"===t)},unbind:function(e,t){for(var n=s(e),o=void 0,r=[],i=void 0,a=0;a<n.length;a++){if(1<(o=n[a].split("+")).length&&(r=l(p,o)),e="*"===(e=o[o.length-1])?"*":w(e),t||(t=m()),!g[e])return;for(var c=0;c<g[e].length;c++)(i=g[e][c]).scope===t&&d(i.mods,r)&&(g[e][c]={})}}};for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(b[a]=r[a]);if("undefined"!=typeof window){var c=window.hotkeys;b.noConflict=function(e){return e&&window.hotkeys===b&&(window.hotkeys=c),b},window.hotkeys=b}return b});
function download(){
var url = window.location.href
if (/pdf/i.test(url.slice(-3))
|| document.querySelectorAll("embed")[0].type === "application/pdf") {
var url_ = url.split('/');
var abs_url = url.slice(0,-4).replace('pdf', 'abs')
// var abs_url = "https://arxiv.org/abs/" + url_[url_.length-1].slice(0,-4);
console.log("Abs url: " + abs_url)
var xhr1 = new XMLHttpRequest();
xhr1.open("GET", abs_url, true)
xhr1.onload = function(e) {
if (xhr1.status === 200) {
var text = xhr1.response;
text = text.replace(/(\r\n\t|\n|\r\t)/gm,"");
// console.log(text);
var regex = /Title:<\/span>(.*)<\/h1><div class="authors">/gm;
var match = regex.exec(text);
if (match != null){
var title = match[1];
title = title.replace(/[<>:"/\\|?*]/g, " ");
console.log("title: "+title);
var xhr2 = new XMLHttpRequest();
// load `document` from `cache`
xhr2.open("GET", "", true);
xhr2.responseType = "blob";
xhr2.onload = function (e) {
if (xhr2.status === 200) {
// `blob` response
var file = window.URL.createObjectURL(xhr2.response);
var a = document.createElement("a");
a.href = file;
a.download = title+'.pdf';
document.body.appendChild(a);
a.click();
// remove `a` following `Save As` dialog,
// `window` regains `focus`
};
};
xhr2.send();
};
};
};
xhr1.send();
console.log("send");
};
}
hotkeys('ctrl+s', function(event,handler) {
switch(handler.key){
case "ctrl+s": console.log('you pressed ctrl+s!'); event.preventDefault(); download(); break;
}
});