commit ae4d423d94758fda4059a6478d064de61f86192e Author: asjiod2 <125102466+asjiod2@users.noreply.github.com> Date: Sat Feb 11 10:05:14 2023 +0900 Add files via upload diff --git a/background.js b/background.js new file mode 100644 index 0000000..94f1e40 --- /dev/null +++ b/background.js @@ -0,0 +1,19 @@ +chrome.runtime.onInstalled.addListener(() => { + chrome.storage.local.set({ isEnabled: false }); +}); + +chrome.commands.onCommand.addListener((command) => { + if (command === "toggle-content-script") { + chrome.storage.local.get(['isEnabled'], (result) => { + const isEnabled = !result.isEnabled; + chrome.storage.local.set({ isEnabled }, () => { + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + for (const tab of tabs) { + chrome.tabs.reload(tab.id); + break; + } + }); + }); + }); + } +}); diff --git a/content-script.js b/content-script.js new file mode 100644 index 0000000..6d2ab6a --- /dev/null +++ b/content-script.js @@ -0,0 +1,55 @@ +function fixNode(node) { + const parent = node.parentElement; + // 親要素が存在しないなら処理しない + if (!parent) { + return; + } + // 親要素がcssかjsだったら処理しない + if (['style', 'script', 'noscript'].includes(parent.tagName.toLowerCase())) { + return; + } + // そもそもテキストではない場合は処理しない + if (node.nodeType !== Node.TEXT_NODE) { + return; + } + // 空のテキストは処理しない + if (!node.textContent.trim()) { + return; + } + // すでにドット化されていたら処理しない + if (/^[\.\s]+$/g.test(node.textContent)) { + return; + } + node.textContent = node.textContent.replace(/\S/g, '.'); +} + +function fixNodeLoop(root) { + for (const node of root.childNodes) { + fixNodeLoop(node); + fixNode(node); + } +} + +function launch() { + const html = document.querySelector('html'); + + fixNodeLoop(html); + + const observer = new MutationObserver((records, observer) => { + for (const record of records) { + fixNodeLoop(record.target); + } + }); + observer.observe(html, { + subtree: true, + childList: true, + attributes: true, + characterData: true, + }); +} + +chrome.storage.local.get(['isEnabled'], (result) => { + if (result.isEnabled) { + launch(); + } +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..187817f --- /dev/null +++ b/manifest.json @@ -0,0 +1,32 @@ +{ + "manifest_version": 3, + "name": "Dotter Freeze", + "version": "4.0", + "description": "文字に疲れた全ての人に捧げる拡張機能です。Ctrlとドット(.)を同時押ししてテキストのない世界へダイブしよう。", + "background": { + "service_worker": "background.js" + }, + "content_scripts": [ + { + "matches": [ + "" + ], + "js": [ + "content-script.js" + ] + } + ], + "commands": { + "toggle-content-script": { + "suggested_key": { + "default": "Ctrl+Period", + "mac": "Command+Period" + }, + "description": "有効/無効を切り替えるZE☆" + } + }, + "permissions": [ + "activeTab", + "storage" + ] +}