corp-tularegion-extension-b.../service_worker.js

72 строки
1.8 KiB
JavaScript

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]
}