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, ) }