-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from xuwei-k/share-query-param
add `share` button
- Loading branch information
Showing
2 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,15 +25,44 @@ import hljs from "https://unpkg.com/@highlightjs/[email protected]/es/highlight. | |
import scala from "https://unpkg.com/@highlightjs/[email protected]/es/languages/scala.min.js"; | ||
hljs.registerLanguage("scala", scala); | ||
|
||
function fromBase64(base64) { | ||
const binString = atob(base64); | ||
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)); | ||
return new TextDecoder().decode(bytes); | ||
} | ||
|
||
function toBase64(text) { | ||
const bytes = new TextEncoder().encode(text); | ||
const binString = Array.from(bytes, (byte) => | ||
String.fromCodePoint(byte), | ||
).join(""); | ||
return btoa(binString); | ||
} | ||
|
||
const getFromStorageOr = (key, defaultValue, fun) => { | ||
const saved = localStorage.getItem(key); | ||
if (saved === null) { | ||
return defaultValue; | ||
} else { | ||
if (fun == null) { | ||
return saved; | ||
const p = new URLSearchParams(location.search); | ||
const fromQuery = p.get(key); | ||
if (fromQuery == null) { | ||
const saved = localStorage.getItem(key); | ||
if (saved === null) { | ||
return defaultValue; | ||
} else { | ||
return fun(saved); | ||
if (fun == null) { | ||
return saved; | ||
} else { | ||
return fun(saved); | ||
} | ||
} | ||
} else { | ||
try { | ||
if (fun == null) { | ||
return fromBase64(fromQuery); | ||
} else { | ||
return fun(fromBase64(fromQuery)); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
return defaultValue; | ||
} | ||
} | ||
}; | ||
|
@@ -369,6 +398,23 @@ const App = () => { | |
clear local storage | ||
</button> | ||
</div> | ||
<div class="col"> | ||
<button | ||
class="btn btn-primary" | ||
id="share" | ||
onclick=${() => | ||
navigator.clipboard.writeText( | ||
window.location.origin + | ||
window.location.pathname + | ||
"?" + | ||
[["source", inputScala]] | ||
.map(([k, v]) => k + "=" + toBase64(v)) | ||
.join("&"), | ||
)} | ||
> | ||
share | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-2"> | ||
|