Skip to content

Instantly share code, notes, and snippets.

@Xeophon
Created August 22, 2023 11:39
Show Gist options
  • Save Xeophon/453e56b91c7e6a1ddcae662b7fcbcfb7 to your computer and use it in GitHub Desktop.
Save Xeophon/453e56b91c7e6a1ddcae662b7fcbcfb7 to your computer and use it in GitHub Desktop.
Arxiv Discussions Tampermonkey Script
// ==UserScript==
// @name Arxiv Twitter Search
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add discussions links to Arxiv
// @author Xeophon + ChatGPT
// @match https://arxiv.org/abs/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
// Set the minimum number of replies and favorites for the Twitter search
let minReplies = 5;
let minFaves = 10;
// Extract the current URL
let currentURL = window.location.href;
let pdfURL = currentURL.replace("/abs/", "/pdf/") + ".pdf";
let paperNumber = currentURL.split("/").pop();
// Extract the title of the paper
let titleElement = document.querySelector("h1.title.mathjax");
let titleText = "";
if (titleElement) {
// Remove the "Title:" descriptor
titleText = titleElement.innerText.replace("Title:", "").trim();
}
// Create the Twitter search URL including the abstract, the PDF, and the title
let twitterSearchURL = `https://twitter.com/search?q=(%22${encodeURIComponent(currentURL)}%22+OR+%22${encodeURIComponent(pdfURL)}%22+OR+%22${encodeURIComponent(titleText)}%22)+min_replies%3A${minReplies}+min_faves%3A${minFaves}`;
// Create the Reddit search URL
let redditSearchURL = `https://www.reddit.com/search/?q=%22${encodeURIComponent(currentURL)}%22+OR+%22${encodeURIComponent(pdfURL)}%22+OR+%22${encodeURIComponent(titleText)}%22&sort=top`;
// Create the Hugging Face URL
let huggingFaceURL = `https://huggingface.co/papers/${paperNumber}`;
// Create the Twitter logo anchor element
let twitterLink = document.createElement("a");
twitterLink.setAttribute("href", twitterSearchURL);
twitterLink.setAttribute("title", "Search discussions on Twitter");
twitterLink.setAttribute("target", "_blank");
twitterLink.innerHTML = `<img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/Logo_of_Twitter.svg" alt="Twitter logo" style="width:20px; margin-right: 5px;">`;
twitterLink.className = "abs-button abs-button-grey abs-button-small"; // Add the same classes as the other links
// Create the Reddit logo anchor element
let redditLink = document.createElement("a");
redditLink.setAttribute("href", redditSearchURL);
redditLink.setAttribute("title", "Search discussions on Reddit");
redditLink.setAttribute("target", "_blank");
redditLink.innerHTML = `<img src="https://static.arxiv.org/static/browse/0.3.4/images/icons/social/reddit.png" alt="Reddit logo" style="width:20px; margin-right: 5px;">`;
redditLink.className = "abs-button abs-button-grey abs-button-small";
// Create the Hugging Face logo anchor element
let huggingFaceLink = document.createElement("a");
huggingFaceLink.setAttribute("href", huggingFaceURL);
huggingFaceLink.setAttribute("title", "View on Hugging Face");
huggingFaceLink.setAttribute("target", "_blank");
huggingFaceLink.innerHTML = `<img src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg" alt="Hugging Face logo" style="width:20px; filter: grayscale(100%); margin-right: 5px;">`; // Added grayscale filter here
huggingFaceLink.className = "abs-button abs-button-grey abs-button-small";
// Check if the Hugging Face URL is valid
GM_xmlhttpRequest({
method: "GET",
url: huggingFaceURL,
onload: function(response) {
if (!response.responseText.includes('<title>404 – Hugging Face</title>')) {
// If it's not a 404 page, remove the grayscale filter to show the colored version of the logo
huggingFaceLink.querySelector("img").style.filter = "none";
}
}
});
// Create the Social Media Discussions section
let socialMediaDiv = document.createElement("div");
socialMediaDiv.className = "bookmarks"; // Use the same class as the "Bookmark" section
socialMediaDiv.style.borderTop = "medium solid #ddd"; // Add the border on top
let titleDiv = document.createElement("div");
titleDiv.innerHTML = "<h3>Social Media Discussions</h3>";
socialMediaDiv.appendChild(titleDiv);
socialMediaDiv.appendChild(twitterLink);
socialMediaDiv.appendChild(redditLink);
socialMediaDiv.appendChild(huggingFaceLink);
// Insert the Social Media Discussions section under the "extra-services" div
let extraServicesDiv = document.querySelector(".extra-services");
if(extraServicesDiv) {
extraServicesDiv.appendChild(socialMediaDiv);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment