diff --git a/content-script.js b/content-script.js new file mode 100644 index 0000000..c24a30f --- /dev/null +++ b/content-script.js @@ -0,0 +1,67 @@ +const corpExtensionLog = (message) => console.log(`corp-tularegion-extension: ${message}`) +let copyInterval + +window.addEventListener('load', () => { + chrome.storage.local.get([ + 'redirectMode', + 'copyMode', + 'autoAuthMode', + ]).then((data) => { + corpExtensionLog(Object.entries(data).map((x) => `${x[0]}=${x[1]}`).join('; ')) + // initCopyMode(data.copyMode) + initAutoAuthMode(data.autoAuthMode) + + // chrome.storage.onChanged.addListener((changes) => { + // if (changes.copyMode) + // initCopyMode(changes.copyMode.newValue) + // }) + }) +}) + +const initCopyMode = (value) => { + if (copyInterval) + clearInterval(copyInterval) + console.log(value) + switch (value) { + case 1: + setCopyInterval(/corp\.tularegion\.(ru)/gis, 'local') + break + + case 2: + setCopyInterval(/corp\.tularegion\.(local)/gis, 'ru') + break + } +} + +const initAutoAuthMode = (value) => { + if (!value) return + const loginField = document.querySelector('input[name="USER_LOGIN"]') + const passwordField = document.querySelector('input[name="USER_PASSWORD"]') + const rememberCheckbox = document.querySelector('input[name="USER_REMEMBER"]') + const authButton = document.querySelector('input.login-btn[type="submit"]') + + if (loginField && passwordField && rememberCheckbox && authButton) { + chrome.storage.local.get([ + 'autoAuthLogin', + 'autoAuthPassword', + ]).then((data) => { + loginField.value = data.autoAuthLogin + passwordField.value = data.autoAuthPassword + rememberCheckbox.checked = true + // authButton.dispatchEvent(new Event('click')) + }) + } +} + +const setCopyInterval = (from, to) => { + copyInterval = setInterval( + () => { + navigator.clipboard.readText() + .then(text => { + console.log(text.replaceAll(from, (match, tld) => match.replace(tld, to))) + }) + .catch() + }, + 1000, + ) +} diff --git a/manifest.json b/manifest.json index 688fce8..15d2d99 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "manifest_version": 3, - "name": "Переадресация Корпоративного портала", + "name": "Корпоративный портал", "version": "1.0", "icons": { "16": "/icons/16x16.png", @@ -15,8 +15,19 @@ "*://corp.tularegion.local/*", "*://corp.tularegion.ru/*" ], + "content_scripts": [ + { + "matches": [ + "*://corp.tularegion.local/*", + "*://corp.tularegion.ru/*" + ], + "js": [ + "/content-script.js" + ] + } + ], "background": { - "service_worker": "/background.js" + "service_worker": "/service_worker.js" }, "action": { "default_popup": "/public/index.html" @@ -32,5 +43,8 @@ }, "content_security_policy": { "extension_pages": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'" + }, + "storage": { + "managed_schema": "/storage_schema.json" } } diff --git a/background.js b/service_worker.js similarity index 95% rename from background.js rename to service_worker.js index fe8ff23..92d1caf 100644 --- a/background.js +++ b/service_worker.js @@ -9,7 +9,8 @@ chrome.runtime.onInstalled.addListener(() => { }) chrome.storage.onChanged.addListener((changes) => { - if (changes.redirectMode) updateRules(changes.redirectMode.newValue) + if (changes.redirectMode) + updateRules(changes.redirectMode.newValue) }) const updateRules = (mode) => { diff --git a/storage_schema.json b/storage_schema.json new file mode 100644 index 0000000..4f8e220 --- /dev/null +++ b/storage_schema.json @@ -0,0 +1,20 @@ +{ + "type": "object", + "properties": { + "redirectMode": { + "type": "integer" + }, + "copyMode": { + "type": "integer" + }, + "autoAuthMode": { + "type": "boolean" + }, + "autoAuthLogin": { + "type": "string" + }, + "autoAuthPassword": { + "type": "string" + } + } +}