Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jessestricker/536366621fbbe247135a94067df979fa to your computer and use it in GitHub Desktop.
Save jessestricker/536366621fbbe247135a94067df979fa to your computer and use it in GitHub Desktop.
Greasemonkey Userscript to set the C++ standard on cppreference.com
// ==UserScript==
// @name Set C++ standard on cppreference.com
// @match https://en.cppreference.com/w/cpp/*
// @icon https://en.cppreference.com/favicon.ico
// ==/UserScript==
const DESIRED_CPP_STANDARD = "C++20";
function changeCppStandard(elem) {
// find option labeled with DESIRED_CPP_STANDARD
let desired_value = "";
for (const option of elem.options) {
if (option.text === DESIRED_CPP_STANDARD) {
desired_value = option.value;
break;
}
}
if (desired_value === "") {
console.warn(
'[userscript] no standard option labeled "' +
DESIRED_CPP_STANDARD +
'" found'
);
return;
}
// set value, dispatch change event
elem.value = desired_value;
elem.dispatchEvent(new Event("change"));
console.info("[userscript] standard set to " + DESIRED_CPP_STANDARD);
}
// define observer callback
const callback = (mutations, observer) => {
for (const mutation of mutations) {
if (mutation.type === "childList") {
// search added children for <select> element
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.className === "stdrev-select") {
const selectElem = node.querySelector("select");
// change C++ standard and discontinue observing
changeCppStandard(selectElem);
observer.disconnect();
}
}
}
}
}
};
// start observing the toolbar
const toolbar = document.querySelector("#cpp-head-tools-right");
const observer = new MutationObserver(callback);
observer.observe(toolbar, { childList: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment