From bebbfb3ad902390b5f35e04e09318dd5664aed17 Mon Sep 17 00:00:00 2001 From: csasq Date: Wed, 18 Dec 2024 12:04:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=BE=20=D1=80=D0=B0=D1=81=D1=88=D0=B8=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D1=83=D0=B7=D0=B5=D1=80=D0=B0=20Chrome?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- background.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.html | 20 +++++++++++++++ manifest.json | 28 +++++++++++++++++++++ popup.js | 3 +++ rules.json | 1 + styles.css | 46 +++++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 background.js create mode 100644 main.html create mode 100644 manifest.json create mode 100644 popup.js create mode 100644 rules.json create mode 100644 styles.css 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; +}