Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

wip: add option delay to check pages #10

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"]
}
]
}
41 changes: 1 addition & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
14 changes: 14 additions & 0 deletions src/helpers/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @returns {Promise<Options>}
*/
const getOptions = async () => {
return await chrome.storage.sync.get(defaultOptions);
}

/**
*
* @param {Options} opts
*/
const setOptions = async (opts) => {
await chrome.storage.sync.set(opts)
}
4 changes: 4 additions & 0 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ const copyTextToClipboard = (text) => {

body.removeChild(textArea);
};

const wait = (secs) => {
return new Promise(resolve => setTimeout(resolve, secs * 1000));
}
8 changes: 7 additions & 1 deletion src/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions src/pages/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Detect zero width characteres extension: Options</title>
</head>
<body>
<div class="content">
<h1>Detect zero width characteres extension: Options</h1>
<div class="option">
<div>Check page delay in secs</div>
<div><input type="number" min="0" id="check_page_delay_secs"></div>
</div>
</div>
<script src="./../helpers/constants.js"></script>
<script src="./../helpers/options.js"></script>
<script src="./options.js"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions src/pages/options.js
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions src/typdef.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/