Skip to content

Instantly share code, notes, and snippets.

@LQ1234
Created January 7, 2022 02:14
Show Gist options
  • Save LQ1234/38c093c54a78629ae6cd58b0bf5ad855 to your computer and use it in GitHub Desktop.
Save LQ1234/38c093c54a78629ae6cd58b0bf5ad855 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name VSCode Server Github Authorization Fixer
// @namespace http://larrys.tech/
// @version 1.0
// @description Fixes callbackUri of vscode auth requests.
// @author Larry Qiu
// @match https://vscode-auth.github.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
const parsedUrl = new URL(window.location.href);
const params = parsedUrl.searchParams;
if(parsedUrl.pathname == "/authorize/" && params.has("callbackUri")){
const callbackUri = decodeURIComponent(params.get("callbackUri"));
if(callbackUri.startsWith("vscode://vscode.github-authentication/did-authenticate")){
console.log("good");
}else{
const replaceUri = "vscode://vscode.github-authentication/did-authenticate?originalcallbackuri="+encodeURIComponent(callbackUri);
params.set("callbackUri", replaceUri);
window.location.replace(parsedUrl.href);
}
}else if(parsedUrl.pathname == "/"){
new MutationObserver((mutations , observer) => {
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode.nodeType == Node.ELEMENT_NODE && addedNode.nodeName == "SCRIPT" && addedNode.innerHTML.includes("window.location.href = ")) {
addedNode.remove();
}
}
}
if(token && token.value.trim().startsWith("vscode://vscode.github-authentication/did-authenticate")){
const url = new URL(token.value.trim());
const params = url.searchParams;
const newurl = new URL(decodeURIComponent(params.get("originalcallbackuri")));
const newparams = newurl.searchParams;
for (const [key, value] of params) {
if(key != "originalcallbackuri") newparams.set(key,value);
}
token.value = newurl.href;
}
}).observe(document.documentElement, { childList: true, subtree: true });
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment