Created
October 11, 2024 21:34
-
-
Save xpl/ec3587e58912a79aedc6edd4077df9b3 to your computer and use it in GitHub Desktop.
Replace "approve" with "bless" in GitHub UI (A Tampermonkey Userscript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GitHub Approve to Bless | |
// @namespace http://tampermonkey.net/ | |
// @version 1.2 | |
// @description Replaces "approve" and its variants with "bless" and corresponding variants on GitHub pull request pages, accounting for dynamic content and case sensitivity. | |
// @author | |
// @match https://github.com/*/*/pull/* | |
// @grant none | |
// @icon https://github.githubassets.com/favicons/favicon.svg | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Map of words to be replaced and their corresponding replacements | |
const wordMap = { | |
'approve': 'bless', | |
'approves': 'blesses', | |
'approved': 'blessed', | |
'approving': 'blessing', | |
'approval': 'blessing', | |
'approvals': 'blessings' // In case "approvals" is used | |
}; | |
// Function to determine if a node should be processed | |
function shouldProcess(node) { | |
const forbiddenTags = ['SCRIPT', 'STYLE', 'TEXTAREA', 'INPUT']; | |
return node.nodeType === Node.TEXT_NODE || | |
(node.nodeType === Node.ELEMENT_NODE && !forbiddenTags.includes(node.nodeName)); | |
} | |
// Function to adjust the case of the replacement word to match the original | |
function adjustCase(original, replacement) { | |
if (original === original.toUpperCase()) { | |
return replacement.toUpperCase(); | |
} else if (original[0] === original[0].toUpperCase()) { | |
return replacement.charAt(0).toUpperCase() + replacement.slice(1); | |
} else { | |
return replacement.toLowerCase(); | |
} | |
} | |
// Function to replace text content | |
function replaceText(node) { | |
if (node.nodeType === Node.TEXT_NODE) { | |
node.textContent = node.textContent.replace(/\b(approve|approves|approved|approving|approval|approvals)\b/gi, (matched) => { | |
const lowerMatched = matched.toLowerCase(); | |
const replacement = wordMap[lowerMatched]; | |
return adjustCase(matched, replacement); | |
}); | |
} else if (shouldProcess(node)) { | |
node.childNodes.forEach(replaceText); | |
} | |
} | |
// Initial text replacement | |
replaceText(document.body); | |
// Observe dynamic content changes | |
const observer = new MutationObserver((mutations) => { | |
mutations.forEach((mutation) => { | |
mutation.addedNodes.forEach((node) => { | |
replaceText(node); | |
}); | |
}); | |
}); | |
observer.observe(document.body, { childList: true, subtree: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment