commit bebbfb3ad902390b5f35e04e09318dd5664aed17 Author: csasq Date: Wed Dec 18 12:04:43 2024 +0300 Разработано расширение для браузера Chrome diff --git a/background.js b/background.js new file mode 100644 index 0000000..fe8ff23 --- /dev/null +++ b/background.js @@ -0,0 +1,70 @@ +const MODES = { + NO_REDIRECT: 0, + RU_TO_LOCAL: 1, + LOCAL_TO_RU: 2, +} + +chrome.runtime.onInstalled.addListener(() => { + chrome.storage.local.set({ redirectMode: MODES.NO_REDIRECT }) +}) + +chrome.storage.onChanged.addListener((changes) => { + if (changes.redirectMode) updateRules(changes.redirectMode.newValue) +}) + +const updateRules = (mode) => { + chrome.declarativeNetRequest.updateEnabledRulesets({ + disableRulesetIds: ['ruleset'], + enableRulesetIds: [], + }, () => { + let rulesToEnable = [] + switch (mode) { + case MODES.RU_TO_LOCAL: + rulesToEnable = [1] + break + + case MODES.LOCAL_TO_RU: + rulesToEnable = [2] + break + } + + chrome.declarativeNetRequest.updateDynamicRules({ + removeRuleIds: [1, 2], + addRules: rulesToEnable.map(id => createRuleById(id)), + }) + }) +} + +const createRuleById = (id) => { + const rules = { + 1: { + id: 1, + priority: 1, + action: { + type: 'redirect', + redirect: { + regexSubstitution: 'https://corp.tularegion.local\\1' + }, + }, + condition: { + regexFilter: '^https://corp\\.tularegion\\.ru(.*)$', + resourceTypes: ['main_frame'], + }, + }, + 2: { + id: 2, + priority: 1, + action: { + type: 'redirect', + redirect: { + regexSubstitution: 'https://corp.tularegion.ru\\1' + }, + }, + condition: { + regexFilter: '^https://corp\\.tularegion\\.local(.*)$', + resourceTypes: ['main_frame'], + }, + }, + } + return rules[id] +} diff --git a/main.html b/main.html new file mode 100644 index 0000000..f4fc26f --- /dev/null +++ b/main.html @@ -0,0 +1,20 @@ + + + + + Переадресация Корпоративного портала + + + +

Переадресация Корпоративного портала

+
+ + + +
+ + + + + + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..3f99085 --- /dev/null +++ b/manifest.json @@ -0,0 +1,28 @@ +{ + "manifest_version": 3, + "name": "Переадресация Корпоративного портала", + "version": "1.0", + "permissions": [ + "declarativeNetRequest", + "storage" + ], + "host_permissions": [ + "*://corp.tularegion.local/*", + "*://corp.tularegion.ru/*" + ], + "background": { + "service_worker": "/background.js" + }, + "action": { + "default_popup": "/main.html" + }, + "declarative_net_request": { + "rule_resources": [ + { + "id": "ruleset", + "enabled": true, + "path": "/rules.json" + } + ] + } +} diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..9c6491f --- /dev/null +++ b/popup.js @@ -0,0 +1,3 @@ +document.querySelector('#ru-to-local').onchange = () => chrome.storage.local.set({ redirectMode: 1 }) +document.querySelector('#local-to-ru').onchange = () => chrome.storage.local.set({ redirectMode: 2 }) +document.querySelector('#no-redirect').onchange = () => chrome.storage.local.set({ redirectMode: 3 }) diff --git a/rules.json b/rules.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/rules.json @@ -0,0 +1 @@ +[] diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..b2bc7ed --- /dev/null +++ b/styles.css @@ -0,0 +1,46 @@ +body { + display: flex; + flex-direction: column; + align-items: center; + padding: 1rem; + width: 24rem; + background-image: linear-gradient(to bottom right, blue, darkblue); + color: white; + font-family: Arial, sans-serif; +} + +input[type="radio"] { + display: none; +} + +h2 { + margin: 0; +} + +#switch-button { + display: flex; + margin-top: 2rem; + width: fit-content; + border: solid .0125rem transparent; + border-radius: 2rem; + box-shadow: + 0 max(.25rem, .25vw) max(.5rem, .5vw) max(.1875rem, .1875vw) rgba(0, 0, 0, .15), + 0 max(.0625rem, .0625vw) max(.1875rem, .1875vw) rgba(0, 0, 0, .3); + overflow: clip; +} + +#switch-button > * { + padding: .5rem 1rem; + background-color: white; + color: black; + font-weight: bold; + opacity: .9; +} + +#switch-button > :hover { + opacity: 1; +} + +#switch-button > :not(:first-child) { + border-left: solid .0125rem black; +}