Skip to content

Instantly share code, notes, and snippets.

@joshparkerj
Last active April 25, 2022 19:36
Show Gist options
  • Select an option

  • Save joshparkerj/3e8e7b624a12c4b4a9fcd3bb349be74f to your computer and use it in GitHub Desktop.

Select an option

Save joshparkerj/3e8e7b624a12c4b4a9fcd3bb349be74f to your computer and use it in GitHub Desktop.
// @author Josh Parker
// ==UserScript==
// @name Show all category links on one page.
// @namespace http://tampermonkey.net/
// @version 0.1
// @description
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/one-page-category.user.js
// @downloadURL https://gist.github.com/joshparkerj/3e8e7b624a12c4b4a9fcd3bb349be74f/raw/one-page-category.user.js
// @updateURL https://gist.github.com/joshparkerj/3e8e7b624a12c4b4a9fcd3bb349be74f/raw/one-page-category.meta.js
// @match https://en.wikipedia.org/wiki/Category:*
// @match https://en.wikipedia.org/w/index.php?title=Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
// ==UserScript==
// @name Show all category links on one page.
// @namespace http://tampermonkey.net/
// @version 0.1
// @description
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/one-page-category.user.js
// @downloadURL https://gist.github.com/joshparkerj/3e8e7b624a12c4b4a9fcd3bb349be74f/raw/one-page-category.user.js
// @updateURL https://gist.github.com/joshparkerj/3e8e7b624a12c4b4a9fcd3bb349be74f/raw/one-page-category.meta.js
// @match https://en.wikipedia.org/wiki/Category:*
// @match https://en.wikipedia.org/w/index.php?title=Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
!function(){let e=document.querySelector("div#mw-pages > div.mw-content-ltr");if(!e){const t=document.createElement("div");t.id="mw-pages",e=document.createElement("div"),e.classList.add("mw-content-ltr"),t.appendChild(e),document.querySelector("div#mw-content-text").after(t)}const t=document.querySelector("div#mw-pages"),n=[...t.childNodes].filter((e=>"A"===e.tagName)),r=n.filter((e=>e.textContent.includes("next"))),l=n.filter((e=>e.textContent.includes("previous"))),i=r.length>0?r[0].href:null,c=l.length>0?l[0].href:null,o=new DOMParser,d=function t(n,r){n&&fetch(n).then((e=>e.text())).then((e=>o.parseFromString(e,"text/html"))).then((n=>{const l=[...n.querySelectorAll("#mw-pages > a")].filter((e=>e.textContent.includes(r))),i=l.length>0?l[0].href:null,c=n.querySelector("#mw-pages > .mw-content-ltr").innerHTML;"next"===r?e.innerHTML+=`<hr>${c}`:"previous"===r&&(e.innerHTML=`${c}<hr>${e.innerHTML}`),t(i,r)}))},s=document.createElement("button");s.innerText="show all",s.addEventListener("click",(()=>{s.setAttribute("disabled",!0),d(i,"next"),d(c,"previous")})),(i||c)&&t.appendChild(s)}();
// ==UserScript==
// @name Show all category links on one page.
// @namespace http://tampermonkey.net/
// @version 0.1
// @description
// @author Josh Parker
// @match https://en.wikipedia.org/wiki/Category:*
// @match https://en.wikipedia.org/w/index.php?title=Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
(function categoryOnePageUserScript() {
let categoryArea = document.querySelector('div#mw-pages > div.mw-content-ltr');
if (!categoryArea) {
const mwPages = document.createElement('div');
mwPages.id = 'mw-pages';
categoryArea = document.createElement('div');
categoryArea.classList.add('mw-content-ltr');
mwPages.appendChild(categoryArea);
document.querySelector('div#mw-content-text').after(mwPages);
}
const categoryPages = document.querySelector('div#mw-pages');
const navLinks = [...categoryPages.childNodes].filter((node) => node.tagName === 'A');
const nextLinks = navLinks.filter((a) => a.textContent.includes('next'));
const previousLinks = navLinks.filter((a) => a.textContent.includes('previous'));
const nextHref = nextLinks.length > 0 ? nextLinks[0].href : null;
const previousHref = previousLinks.length > 0 ? previousLinks[0].href : null;
const parser = new DOMParser();
const getMorePages = function getMorePages(href, direction) {
if (!href) {
return;
}
fetch(href)
.then((r) => r.text())
.then((text) => parser.parseFromString(text, 'text/html'))
.then((doc) => {
const docLinks = [...doc.querySelectorAll('#mw-pages > a')].filter((a) => a.textContent.includes(direction));
const docHref = docLinks.length > 0 ? docLinks[0].href : null;
const docCategoryArea = doc.querySelector('#mw-pages > .mw-content-ltr').innerHTML;
if (direction === 'next') {
categoryArea.innerHTML += `<hr>${docCategoryArea}`;
} else if (direction === 'previous') {
categoryArea.innerHTML = `${docCategoryArea}<hr>${categoryArea.innerHTML}`;
}
getMorePages(docHref, direction);
});
};
const getAllLaterPages = (href) => getMorePages(href, 'next');
const getAllEarlierPages = (href) => getMorePages(href, 'previous');
const showAllButton = document.createElement('button');
showAllButton.innerText = 'show all';
showAllButton.addEventListener('click', () => {
showAllButton.setAttribute('disabled', true);
getAllLaterPages(nextHref);
getAllEarlierPages(previousHref);
});
if (nextHref || previousHref) {
categoryPages.appendChild(showAllButton);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment