Skip to content

Instantly share code, notes, and snippets.

@hexium310
Last active September 25, 2021 13:38
Show Gist options
  • Save hexium310/76eda31e03f7bd09227133a26364bbe8 to your computer and use it in GitHub Desktop.
Save hexium310/76eda31e03f7bd09227133a26364bbe8 to your computer and use it in GitHub Desktop.
This userscript adds a button for switching plain/rich for a Markdown blob on GitHub.
// ==UserScript==
// @name Switching Plain for Markdown on GitHub
// @namespace hexium310
// @version 0.3
// @description This userscript adds a button for switching plain/rich for a Markdown blob on GitHub.
// @author Hexin
// @match https://*.github.com/*/*
// @downloadURL https://gist.github.com/hexium310/76eda31e03f7bd09227133a26364bbe8/raw/switch-plain-md-github.user.js
// @updateURL https://gist.github.com/hexium310/76eda31e03f7bd09227133a26364bbe8/raw/switch-plain-md-github.user.js
// @grant none
// ==/UserScript==
(() => {
'use strict';
const toCamelCase = (str) => str.replace(/^./, (v) => v.toUpperCase());
const addButton = () => {
const rawButton = document.getElementById('raw-url');
if (!rawButton) {
return;
}
const url = new URL(location.href);
const newButton = rawButton.cloneNode(true);
const isPlain = url.searchParams.has('plain');
const kind = isPlain ? 'rich' : 'plain';
if (isPlain) {
url.searchParams.delete('plain');
} else {
url.searchParams.set('plain', 1);
}
newButton.id = `${ kind }-url`;
newButton.textContent = ` ${ toCamelCase(kind) } `;
newButton.href = url;
rawButton.parentNode.insertBefore(newButton, rawButton);
};
addButton();
document.addEventListener('pjax:end', addButton);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment