Created
May 14, 2022 09:38
-
-
Save jaysonsantos/80ef18c1baeb86ef96ccdb89e8b5dfcd to your computer and use it in GitHub Desktop.
Enable the download of simple applets to jnlp format
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 Convert applet to downloadable jnlp | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match http://192.168.1.23/kvms.html | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=1.23 | |
// @grant none | |
// @require https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const template = `<?xml version="1.0" encoding="UTF-8"?> | |
<jnlp spec="1.0+" codebase="{{ codebase }}" href=""> | |
<information> | |
<title>Launch applet with Web Start</title> | |
<vendor>Foo Bar Inc.</vendor> | |
<offline-allowed/> | |
</information> | |
<resources> | |
<jar href="{{ jarHref }}" main="true" /> | |
</resources> | |
<applet-desc main-class="{{ mainClass }}" width="300" height="200" name="egal"> | |
{{#each params}} | |
<param name="{{ name }}" value="{{ value }}"/> | |
{{/each}} | |
</applet-desc> | |
<security> | |
<all-permissions /> | |
</security> | |
<update check="background"/> | |
</jnlp> | |
`; | |
const applets = document.querySelectorAll('applet'); | |
const downloadAsJnlp = (applet) => { | |
const data = { | |
codebase: new URL("./", document.baseURI).toString(), | |
mainClass: applet.attributes.code.textContent, | |
jarHref: applet.attributes.archive.textContent, | |
params: parseParams(applet), | |
}; | |
const render = Handlebars.compile(template); | |
const blob = new Blob([render(data)], {type: "octet/stream"}); | |
const href = window.URL.createObjectURL(blob); | |
const name = 'applet.jnlp'; | |
return {href, name}; | |
} | |
const parseParams = (applet) => { | |
return Array.from(applet.querySelectorAll('param')).map(parseParam); | |
} | |
const parseParam = (param) => { | |
return { | |
name: param.name, | |
value: param.value, | |
} | |
} | |
const createDownloadLinks = (applets) => { | |
const count = applets.length; | |
console.log(`Found ${count} applets`); | |
for (let i = 0; i < count; i++) { | |
createDownloadLink(applets[i]); | |
} | |
} | |
const createDownloadLink = (applet) => { | |
const downloadLink = document.createElement('a'); | |
const {href, name} = downloadAsJnlp(applet); | |
downloadLink.href = href; | |
downloadLink.download = name; | |
downloadLink.appendChild(document.createTextNode("Download as jnlp")); | |
applet.prepend(downloadLink); | |
} | |
createDownloadLinks(applets); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment