Skip to content

Instantly share code, notes, and snippets.

@TTTPOB
Last active April 17, 2024 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TTTPOB/33a02faf1b3f7d0a5a693022de214938 to your computer and use it in GitHub Desktop.
Save TTTPOB/33a02faf1b3f7d0a5a693022de214938 to your computer and use it in GitHub Desktop.
reveal nature journal names in google search results
// ==UserScript==
// @name Nature Article Journal Modifier for Google Search
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Modify Nature article titles in Google Search results to display their journal names, using Fetch API
// @author tttpob
// @match https://www.google.com/search?*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const doiPrefix = '10.1038/';
async function processArticleLink(articleLink) {
// Skip if the link has already been marked
if (articleLink.getAttribute('journal-checked')) {
return
};
articleLink.setAttribute("journal-checked", "loading");
const match = articleLink.href.match(/https:\/\/www\.nature\.com\/articles\/(.+)$/);
if (match) {
const articleId = match[1];
const doi = `${doiPrefix}${articleId}`;
const journalName = await fetchJournalName(doi);
if (journalName) {
// Store the journal name in the attribute for future reference
articleLink.setAttribute('journal-checked', journalName);
modifyLinkTitle(articleLink, journalName);
}
}
}
async function fetchJournalName(doi) {
try {
const response = await fetch(`https://api.crossref.org/works/${doi}`);
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
const journalName = data.message["container-title"];
return journalName;
} catch (error) {
console.error('Error fetching journal name:', error);
return null;
}
}
function modifyLinkTitle(articleLink, journalName) {
// Find the h3 element that is a child of the link's parent container
const titleElement = articleLink.closest('.g').querySelector('h3');
if (titleElement && !titleElement.textContent.startsWith(journalName)) {
// Replace "Nature" at the end of the title with the journal name, if present
titleElement.textContent = titleElement.textContent.replace(/^/, `${journalName} - `);
}
}
function processSearchResults() {
const links = document.querySelectorAll('a');
links.forEach(link => {
if (link.href.startsWith('https://www.nature.com/articles/')) {
let timeToSleep = Math.floor(Math.random() * 1000);
setTimeout(() => processArticleLink(link), timeToSleep);
}
});
}
function observeDOMChanges() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
processSearchResults();
}
});
});
const config = {
childList: true,
subtree: true
};
const targetNode = document.querySelector('body');
if (targetNode) {
observer.observe(targetNode, config);
}
}
window.addEventListener('load', () => {
setTimeout(() => {
processSearchResults();
observeDOMChanges();
}, 1500)
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment