Skip to content

Instantly share code, notes, and snippets.

@mems
Last active April 22, 2022 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mems/e7277c1118e1d876dd7333bb8c2bb49b to your computer and use it in GitHub Desktop.
Save mems/e7277c1118e1d876dd7333bb8c2bb49b to your computer and use it in GitHub Desktop.
[Userscript] Microsoft Remote Desktop Web Access for all
// ==UserScript==
// @name Microsoft Remote Desktop Web Access for all
// @namespace memmie.lenglet.name
// @author mems <memmie@lenglet.name>
// @homepageURL https://gist.github.com/mems/e7277c1118e1d876dd7333bb8c2bb49b
// @description Allow to use RD Web Access on browser that don't support RD ActiveX
// @match *://*/RDWeb/Pages/*
// @updateURL https://gist.github.com/mems/e7277c1118e1d876dd7333bb8c2bb49b/raw/msrdwa-for-all.user.js
// @version 1.0.0
// ==/UserScript==
// Fix CSS included by JS
CSSStyleSheet.prototype.addRule === undefined && Array.from(document.scripts).forEach(s => {let r = /document\.styleSheets\[\d+\]\.addRule\('((?:\\'|[^'])+)',\s'((?:\\'|[^'])+)'/g; for(let m; (m = r.exec(s.text)) !== null;) document.styleSheets[0].insertRule(m[1]+"{"+m[2])+"}"});
// Show the appboard and public mode checkbox
Array.from(document.querySelectorAll(".tswa_appboard, .tswa_PublicCheckboxLess")).forEach(el => el.style.display = !el.classList.contains("tswa_appboard") || el.id.endsWith("AppDisplay") ? "block" : "none");
// Inject RDP object proxy (available only in IE with RD ActiveX)
const script = document.createElement('script');
script.appendChild(document.createTextNode(`
window.MsRdpClientShell = window.MsRdpClientShell || {
PublicMode: true,
RdpFileContents: "",
Launch(){
const settings = this.RdpFileContents.split("\\r\\n").reduce((settings, line) => {
if(line.trim()){
const [type, ...value] = line.split(":");
settings.set(type, value.join(":"));
}
return settings;
}, new Map());
settings.set("public mode", this.PublicMode ? "i:1" : "i:0");
const a = document.createElement("a");
const raw = Array.from(settings).map(([key, value]) => key + ":" + value).join("\\r\\n");
const url = a.href = URL.createObjectURL(new Blob([raw], {type: "application/x-rdp"}));
const appname = (settings.get("remoteapplicationprogram") || "").startsWith("s:||") ? settings.get("remoteapplicationprogram").substr(4) : (settings.get("remoteapplicationname") || "s:remoteapp").substr(2);
const wksp = (settings.get("Workspace Id") || "s:").substr(2);
const title = [appname, wksp].filter(v => v).join(" - ");
a.download = title.replace(/[\\\\\\/:\\*\\?"<>\\|]/g, "-") + ".rdp";// valid Windows filename
document.body.appendChild(a);
a.click();
a.remove();
this.releaseURL(url);// no retains
},
releaseURL(url){
setTimeout(() => URL.revokeObjectURL(url), 10000);// just in case
}
}
`));
document.head.appendChild(script);