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/c67bbcfe9dac51a27e30193b061c65fe to your computer and use it in GitHub Desktop.

Select an option

Save joshparkerj/c67bbcfe9dac51a27e30193b061c65fe to your computer and use it in GitHub Desktop.
count svgs on each page
// ==UserScript==
// @name count svgs on each page
// @namespace http://tampermonkey.net/
// @version 0.1
// @description count svgs on each page
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/count-svgs.user.js
// @downloadURL https://gist.github.com/joshparkerj/c67bbcfe9dac51a27e30193b061c65fe/raw/count-svgs.user.js
// @updateURL https://gist.github.com/joshparkerj/c67bbcfe9dac51a27e30193b061c65fe/raw/count-svgs.meta.js
// @match https://en.wikipedia.org/wiki/Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
// ==UserScript==
// @name count svgs on each page
// @namespace http://tampermonkey.net/
// @version 0.1
// @description count svgs on each page
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/count-svgs.user.js
// @downloadURL https://gist.github.com/joshparkerj/c67bbcfe9dac51a27e30193b061c65fe/raw/count-svgs.user.js
// @updateURL https://gist.github.com/joshparkerj/c67bbcfe9dac51a27e30193b061c65fe/raw/count-svgs.meta.js
// @match https://en.wikipedia.org/wiki/Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
!function(){const e=[...document.querySelectorAll("#mw-content-text #mw-pages .mw-content-ltr a[title]")],t=new DOMParser,n=document.createElement("button");n.addEventListener("click",(()=>{e.forEach((e=>{const{href:n}=e;fetch(n).then((e=>e.text())).then((e=>t.parseFromString(e,"text/html"))).then((e=>e.querySelectorAll("a[href$=svg]").length)).then((t=>{const n=document.createElement("p"),c=document.createElement("em");n.appendChild(c),c.innerText=`${t} svg link${1===t?"":"s"} found`,e.appendChild(n)}))}))})),n.innerText="get number of svgs on each page",document.querySelector("#mw-pages > h2 ~ p").appendChild(n)}();
// ==UserScript==
// @name get number of svgs on each page
// @namespace http://tampermonkey.net/
// @version 0.1
// @description get number of svgs on each page
// @author Josh Parker
// @match https://en.wikipedia.org/wiki/Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
(function getNumberOfSvgs() {
const pageLinks = [...document.querySelectorAll('#mw-content-text #mw-pages .mw-content-ltr a[title]')];
const parser = new DOMParser();
const button = document.createElement('button');
button.addEventListener('click', () => {
pageLinks.forEach((pageLink) => {
const { href } = pageLink;
fetch(href)
.then((r) => r.text())
.then((text) => parser.parseFromString(text, 'text/html'))
.then((dom) => dom.querySelectorAll('a[href$=svg]').length)
.then((count) => {
const p = document.createElement('p');
const em = document.createElement('em');
p.appendChild(em);
em.innerText = `${count} svg link${count === 1 ? '' : 's'} found`;
pageLink.appendChild(p);
});
});
});
button.innerText = 'get number of svgs on each page';
document.querySelector('#mw-pages > h2 ~ p').appendChild(button);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment