|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/*jslint white:true */ |
| 4 | + |
| 5 | +function minify(js) { |
| 6 | + return ( |
| 7 | + "javascript:(function()%7B" + |
| 8 | + encodeURIComponent( |
| 9 | + js |
| 10 | + .replace(/[\r\n\t]+/gm, " ") |
| 11 | + .replace(/\x20+/gm, " ") |
| 12 | + .trim(), |
| 13 | + ) + |
| 14 | + "%7D)()" |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +function normalize(s) { |
| 19 | + return s |
| 20 | + .replaceAll( |
| 21 | + /[^a-zA-Z0-9\x21$\x26\x27()*+\x2c\x2d./\x3a\x3b\x3d?\x40_]/g, |
| 22 | + "-", |
| 23 | + ) |
| 24 | + .replaceAll(/\x2d+/g, "-"); |
| 25 | +} |
| 26 | + |
| 27 | +// init() is used in HTML |
| 28 | +// eslint-disable-next-line no-unused-vars |
| 29 | +async function init(path) { |
| 30 | + document.getElementById("mininame").oninput = function () { |
| 31 | + document.getElementById("minified").textContent = |
| 32 | + document.getElementById("mininame").value; |
| 33 | + }; |
| 34 | + document.getElementById("plaintext").oninput = function () { |
| 35 | + document.getElementById("minified").href = minify( |
| 36 | + document.getElementById("plaintext").value, |
| 37 | + ); |
| 38 | + }; |
| 39 | + var content = []; |
| 40 | + console.debug("Fetching index"); |
| 41 | + var index = await fetch( |
| 42 | + "https://raw.githubusercontent.com/" + |
| 43 | + path + |
| 44 | + "/main/javascripts/index.json", |
| 45 | + ) |
| 46 | + .then(function (response) { |
| 47 | + return response.json(); |
| 48 | + }) |
| 49 | + .catch(console.error); |
| 50 | + console.debug("Fetched index"); |
| 51 | + content.push(document.createElement("hr")); |
| 52 | + var toc = document.createElement("section"); |
| 53 | + content.push(toc); |
| 54 | + toc.id = "toc"; |
| 55 | + var ttoc = document.createElement("h3"); |
| 56 | + toc.appendChild(ttoc); |
| 57 | + ttoc.textContent = "Examples (" + index.length + ")"; |
| 58 | + var ul = document.createElement("ul"); |
| 59 | + toc.appendChild(ul); |
| 60 | + var i; |
| 61 | + for (i = 0; i < index.length; i++) { |
| 62 | + var li = document.createElement("li"); |
| 63 | + ul.appendChild(li); |
| 64 | + li.innerHTML = |
| 65 | + '<a href="#' + |
| 66 | + encodeURIComponent(normalize(index[i].name)) + |
| 67 | + '">' + |
| 68 | + index[i].name + |
| 69 | + "</a>"; |
| 70 | + } |
| 71 | + var loading = document.getElementById("loading"); |
| 72 | + var j; |
| 73 | + for (j = 0; j < index.length; j++) { |
| 74 | + loading.textContent += "."; |
| 75 | + content.push(document.createElement("hr")); |
| 76 | + console.debug("Adding " + index[j].name); |
| 77 | + var bookmarklet = document.createElement("section"); |
| 78 | + content.push(bookmarklet); |
| 79 | + bookmarklet.className = "bookmarklet"; |
| 80 | + var title = document.createElement("h3"); |
| 81 | + bookmarklet.appendChild(title); |
| 82 | + var tlink = document.createElement("a"); |
| 83 | + title.appendChild(tlink); |
| 84 | + tlink.className = "name"; |
| 85 | + tlink.id = normalize(index[j].name); |
| 86 | + tlink.textContent = index[j].name; |
| 87 | + var desc = document.createElement("h4"); |
| 88 | + bookmarklet.appendChild(desc); |
| 89 | + desc.textContent = "Description"; |
| 90 | + var description = document.createElement("p"); |
| 91 | + bookmarklet.appendChild(description); |
| 92 | + description.textContent = index[j].description; |
| 93 | + console.debug("Fetching source code for " + index[j].name); |
| 94 | + var js = await fetch( |
| 95 | + "https://raw.githubusercontent.com/" + |
| 96 | + path + |
| 97 | + "/main/javascripts/" + |
| 98 | + encodeURIComponent(index[j].name) + |
| 99 | + ".js", |
| 100 | + ) |
| 101 | + .then(function (response) { |
| 102 | + return response.text(); |
| 103 | + }) |
| 104 | + .catch(console.error); |
| 105 | + console.debug("Fetched source code for " + index[j].name); |
| 106 | + var book = document.createElement("h4"); |
| 107 | + bookmarklet.appendChild(book); |
| 108 | + book.textContent = "Bookmarklet"; |
| 109 | + var instructions = document.createElement("p"); |
| 110 | + bookmarklet.appendChild(instructions); |
| 111 | + instructions.innerHTML = |
| 112 | + 'Drag and drop or bookmark this link: <a href="' + |
| 113 | + minify(js) + |
| 114 | + '">' + |
| 115 | + index[j].name + |
| 116 | + "</a>"; |
| 117 | + var code = document.createElement("h4"); |
| 118 | + bookmarklet.appendChild(code); |
| 119 | + code.textContent = "Source code"; |
| 120 | + var pre = document.createElement("pre"); |
| 121 | + bookmarklet.appendChild(pre); |
| 122 | + pre.className = "highlight"; |
| 123 | + var source = document.createElement("code"); |
| 124 | + pre.appendChild(source); |
| 125 | + source.textContent = js.trim(); |
| 126 | + var edit = document.createElement("a"); |
| 127 | + bookmarklet.appendChild(edit); |
| 128 | + edit.textContent = "Edit it!"; |
| 129 | + edit.href = "#editor"; |
| 130 | + edit.onclick = (function (a1, a2) { |
| 131 | + return function () { |
| 132 | + var e1 = document.getElementById("plaintext"); |
| 133 | + e1.value = a1; |
| 134 | + e1.dispatchEvent(new Event("input")); |
| 135 | + var e2 = document.getElementById("mininame"); |
| 136 | + e2.value = a2; |
| 137 | + e2.dispatchEvent(new Event("input")); |
| 138 | + }; |
| 139 | + })(js, index[j].name); |
| 140 | + } |
| 141 | + content.push(document.createElement("hr")); |
| 142 | + document.getElementById("bookmarklets-list").replaceChildren(...content); |
| 143 | +} |
0 commit comments