Skip to content

Instantly share code, notes, and snippets.

@Benricheson101
Last active November 7, 2023 13:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benricheson101/31fcb44102a102bbaf0554f479985775 to your computer and use it in GitHub Desktop.
Save Benricheson101/31fcb44102a102bbaf0554f479985775 to your computer and use it in GitHub Desktop.
Toggle Discord OAuth2 scopes on the authorization page or the in-app auth screen
// ==UserScript==
// @name Toggle Discord OAuth Scopes
// @version 0.2
// @description Toggle Discord OAuth scopes by clicking them on the auth screen
// @author Ben Richeson (github.com/benricheson101)
// @match https://discord.com/oauth2/authorize?*
// @match https://*.discord.com/oauth2/authorize?*
// @match https://discord.com/channels/*
// @match https://*.discord.com/channels/*
// ==/UserScript==
(() => {
// prevents running multiple times
if (window.oauthScopeToggleEnabled) {
return;
}
const run = () => {
const scopeX = document.querySelector('div[class^="scopeTimes"]');
const scopeCheck = document.querySelector('div[class^="scopeCheck"');
const c = document.querySelector('div[class*="authorize"]');
const fiberKey = Object.keys(scopeX).find(k => k.startsWith('__reactFiber$'));
const toggleScope = scopeNode => {
const scope = scopeNode[fiberKey].key;
const filterInPlace = (a, f) => a.splice(0, a.length, ...a.filter(f));
const scopes = c[fiberKey].return.memoizedState.memoizedState[0];
if (scopes.includes(scope)) {
filterInPlace(scopes, s => s !== scope);
scopeNode.replaceChild(scopeX.cloneNode(true), scopeNode.childNodes[0]);
} else {
scopes.push(scope);
scopeNode.replaceChild(
scopeCheck.cloneNode(true),
scopeNode.childNodes[0]
);
}
};
document
.querySelectorAll('div[class^="scopes"] > div[class^="scope"]')
.forEach(scope => {
const key = scope[fiberKey].key;
if (!key) {
return;
}
scope.style.cursor = 'pointer';
scope.onclick = () => {
toggleScope(scope);
};
});
};
const isLoaded = () =>
!!document.querySelector('div[class^="scopes"] > div[class^="scope"]');
let prev = isLoaded();
if (prev) {
run();
}
const observer = new MutationObserver(() => {
const curr = isLoaded();
if (!prev && curr) {
run();
}
prev = curr;
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
window.oauthScopeToggleEnabled = true;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment