Skip to content

Instantly share code, notes, and snippets.

@tshu-w
Last active January 16, 2021 08:15
Show Gist options
  • Save tshu-w/69e9fae4e632993d96475523435b219b to your computer and use it in GitHub Desktop.
Save tshu-w/69e9fae4e632993d96475523435b219b to your computer and use it in GitHub Desktop.
arXiv title fixer
// ==UserScript==
// @name Arxiv titlatorizer
// @namespace Titlatorizer
// @version 0.1
// @description Set correct tab <title> on arXiv pages.
// @author Tianshu Wang
// @match https://arxiv.org/pdf/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
//
// Based on
// https://github.com/musically-ut/arXiv-title-fixer &
// https://gist.github.com/o-jasper/df6f93e8c1a9ec8d3ff59f0266dfe368
// (c) 2021 Tianshu Wang
// This code is licensed under MIT license
//
(function() {
'use strict';
const TITLE_PLACEHOLDER = 'Unknown title.';
function getPaperTitleFromResponse(feed) {
var paperTitle = null;
Array.prototype.slice.call(feed.children).forEach(function (child) {
if (child.nodeName.toLowerCase() === 'entry') {
var entry = child;
Array.prototype.slice.call(entry.children).forEach(function (field) {
if (field.nodeName.toLowerCase() === 'title') {
var title = field;
paperTitle = title.childNodes[0].wholeText;
}
});
}
});
return (paperTitle === null) ? TITLE_PLACEHOLDER : paperTitle;
}
function addTitleToHead(paperTitle) {
var title = document.createElement('title');
title.appendChild(document.createTextNode(paperTitle));
var head = null;
if (document.head) {
Array.prototype.slice.call(document.head.children).forEach(function (child) {
if (child.nodeName.toLowerCase() === 'title') {
document.head.removeChild(child);
}
});
document.head.appendChild(title);
} else {
head = document.createElement('head');
head.appendChild(title);
var html = document.body.parentElement;
html.insertBefore(head, document.body);
}
}
var paperId = null;
try {
var pathComponents = window.location.pathname.split('/');
var pdfName = pathComponents[pathComponents.length - 1];
paperId = /([^v]*)(v[0-9]*)?\.pdf/.exec(pdfName)[1];
} catch(e) {
console.warn('Could not get submission ID. Error: ', e);
}
if (paperId !== null) {
GM_xmlhttpRequest({
method:'GET',
url:"http://export.arxiv.org/api/query?id_list=" + paperId,
onload:function (response) {
if (response.status === 200) {
var responseXML = (new DOMParser()).parseFromString(response.responseText, "text/xml");
var paperTitle = getPaperTitleFromResponse(responseXML.childNodes[0]);
addTitleToHead(paperTitle);
};
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment