mirror of
https://codeberg.org/vlw/code-export-syntax.git
synced 2025-11-05 04:22:43 +01:00
52 lines
No EOL
1.1 KiB
JavaScript
52 lines
No EOL
1.1 KiB
JavaScript
const KEY_STORAGE_TNX = "tnx";
|
|
|
|
const SELECTOR = Object.freeze({
|
|
COPY : "#copy",
|
|
HTML : "#html",
|
|
CLOSE : "#close"
|
|
});
|
|
|
|
class ExportToHtml {
|
|
constructor() {
|
|
this.#setContent();
|
|
|
|
this.#bindClose();
|
|
this.#bindCopyHtml();
|
|
}
|
|
|
|
/**
|
|
* Copy the contents of the textarea element onto the clipboard
|
|
*
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
async copyHtml() {
|
|
return await navigator.clipboard.writeText(document.querySelector(SELECTOR.HTML).value);
|
|
}
|
|
|
|
/**
|
|
* Set the textarea value with HTML from session storage
|
|
*/
|
|
async #setContent() {
|
|
const content = await chrome.storage.session.get(KEY_STORAGE_TNX);
|
|
|
|
document.querySelector(SELECTOR.HTML).value = content[KEY_STORAGE_TNX];
|
|
}
|
|
|
|
/**
|
|
* Bind button for copying textarea value
|
|
*/
|
|
#bindCopyHtml() {
|
|
document.body.querySelector(SELECTOR.COPY).addEventListener("click", () => {
|
|
this.copyHtml() ? alert("HTML Copied!") : alert("Failed to copy HTML");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Bind button for closing this export window
|
|
*/
|
|
#bindClose() {
|
|
document.querySelector(SELECTOR.CLOSE).addEventListener("click", () => window.close());
|
|
}
|
|
}
|
|
|
|
globalThis._ExportToHtml = new ExportToHtml(); |