diff --git a/manifest.json b/manifest.json index d1b3dd6..188e665 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,8 @@ "manifest_version": 3, "description": "Detects zero-width characters, highlights the characters and containing DOM element, and allows sanitization and copying of text.", "homepage_url": "https://github.com/roymckenzie/detect-zero-width-characters-chrome-extension", - "permissions": ["contextMenus", "clipboardWrite"], + "permissions": ["contextMenus", "clipboardWrite", "storage"], + "options_page": "src/pages/options.html", "background": { "service_worker": "src/background/service-worker.js" }, @@ -20,7 +21,7 @@ }, { "matches": ["http://*/*", "https://*/*"], - "js": ["src/helpers/constants.js", "src/helpers/utils.js", "src/inject/inject.js"] + "js": ["src/helpers/constants.js", "src/helpers/utils.js", "src/helpers/options.js", "src/inject/inject.js"] } ] } diff --git a/package-lock.json b/package-lock.json index ac37508..cff5733 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,45 +1,6 @@ { - "name": "detect-zero-width-characters-chrome-extension", - "lockfileVersion": 2, "requires": true, - "packages": { - "": { - "devDependencies": { - "@types/chrome": "^0.0.248" - } - }, - "node_modules/@types/chrome": { - "version": "0.0.248", - "resolved": "https://registry.npmjs.org/@types/chrome/-/chrome-0.0.248.tgz", - "integrity": "sha512-qtBzxZD1v3eWZn8XxH1i07pAhzJDHnxJBBVy7bmntXxXKxjzNXYxD41teqa5yOcX/Yy8brRFGZESEzGoINvBDg==", - "dev": true, - "dependencies": { - "@types/filesystem": "*", - "@types/har-format": "*" - } - }, - "node_modules/@types/filesystem": { - "version": "0.0.34", - "resolved": "https://registry.npmjs.org/@types/filesystem/-/filesystem-0.0.34.tgz", - "integrity": "sha512-La4bGrgck8/rosDUA1DJJP8hrFcKq0BV6JaaVlNnOo1rJdJDcft3//slEbAmsWNUJwXRCc0DXpeO40yuATlexw==", - "dev": true, - "dependencies": { - "@types/filewriter": "*" - } - }, - "node_modules/@types/filewriter": { - "version": "0.0.31", - "resolved": "https://registry.npmjs.org/@types/filewriter/-/filewriter-0.0.31.tgz", - "integrity": "sha512-12df1utOvPC80+UaVoOO1d81X8pa5MefHNS+gWX9R2ucSESpMz9K5QwlTWDGKASrzCpSFwj7NPYh+nTsolgEGA==", - "dev": true - }, - "node_modules/@types/har-format": { - "version": "1.2.14", - "resolved": "https://registry.npmjs.org/@types/har-format/-/har-format-1.2.14.tgz", - "integrity": "sha512-pEmBAoccWvO6XbSI8A7KvIDGEoKtlLWtdqVCKoVBcCDSFvR4Ijd7zGLu7MWGEqk2r8D54uWlMRt+VZuSrfFMzQ==", - "dev": true - } - }, + "lockfileVersion": 1, "dependencies": { "@types/chrome": { "version": "0.0.248", diff --git a/src/helpers/constants.js b/src/helpers/constants.js index eb2d5e4..23fba55 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -4,3 +4,11 @@ * @since 0.0.1 */ const zeroWidthCharacterCodes = [8203, 8204, 8205, 8288]; + +/** @type {Options} */ +const defaultOptions = { + CHECK_PAGE_IN_SECS: { + name: "check_page_delay_secs", + value: 0, + }, +} \ No newline at end of file diff --git a/src/helpers/options.js b/src/helpers/options.js new file mode 100644 index 0000000..d7fdf93 --- /dev/null +++ b/src/helpers/options.js @@ -0,0 +1,14 @@ +/** + * @returns {Promise} + */ +const getOptions = async () => { + return await chrome.storage.sync.get(defaultOptions); +} + +/** + * + * @param {Options} opts + */ +const setOptions = async (opts) => { + await chrome.storage.sync.set(opts) +} \ No newline at end of file diff --git a/src/helpers/utils.js b/src/helpers/utils.js index df4a878..a0223ad 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -32,3 +32,7 @@ const copyTextToClipboard = (text) => { body.removeChild(textArea); }; + +const wait = (secs) => { + return new Promise(resolve => setTimeout(resolve, secs * 1000)); +} \ No newline at end of file diff --git a/src/inject/inject.js b/src/inject/inject.js index 0e46a81..19f9371 100644 --- a/src/inject/inject.js +++ b/src/inject/inject.js @@ -73,7 +73,13 @@ * * @since 0.0.1 */ - const checkPage = () => { + const checkPage = async () => { + const opts = await getOptions() + + if (opts.CHECK_PAGE_IN_SECS.value > 0) { + await wait(opts.CHECK_PAGE_IN_SECS.value) + } + const allElements = document.getElementsByTagName("*"); [...allElements].forEach(checkElement); diff --git a/src/pages/options.html b/src/pages/options.html new file mode 100644 index 0000000..998470a --- /dev/null +++ b/src/pages/options.html @@ -0,0 +1,20 @@ + + + + + + Detect zero width characteres extension: Options + + +
+

Detect zero width characteres extension: Options

+
+
Check page delay in secs
+
+
+
+ + + + + \ No newline at end of file diff --git a/src/pages/options.js b/src/pages/options.js new file mode 100644 index 0000000..d4a331f --- /dev/null +++ b/src/pages/options.js @@ -0,0 +1,27 @@ +const saveOptions = async (e) => { + e.target.disabled = true + const value = e.target.value + + await setOptions({ + CHECK_PAGE_IN_SECS: { + name: e.target.id, + value, + }, + }) + + e.target.disabled = false +}; + + +const restoreOptions = async () => { + const opts = await getOptions() + + Object.values(opts).forEach((opt) => { + if (opt.name === "check_page_delay_secs") { + document.querySelector('#check_page_delay_secs').value = opt.value + } + }) +}; + +document.addEventListener('DOMContentLoaded', restoreOptions); +document.querySelector('#check_page_delay_secs').addEventListener('change', saveOptions); \ No newline at end of file diff --git a/src/typdef.js b/src/typdef.js index c0fce7a..cf6b5f1 100644 --- a/src/typdef.js +++ b/src/typdef.js @@ -12,3 +12,17 @@ * @property {"sanitizeAndCopyContextMenuItemAction"} type * @property {string} text - The text to copy to clipboard. */ + + +/** + * @typedef {object} Options + * + * @property {Option} CHECK_PAGE_IN_SECS + */ + +/** + * @typedef {object} Option + * + * @property {string} name + * @property {*} value + */