document.getElementById('import-cookies').addEventListener('click', () => { if…
- User
document.getElementById('import-cookies').addEventListener('click', () => { if (disableButtons) { return; }
setPageTitle('Cookie Editor Plus - Import Cookies'); disableButtons = true; Animate.transitionPage(containerCookie, containerCookie.firstChild, createHtmlFormImport(), 'left', () => { disableButtons = false; }); document.getElementById('button-bar-default').classList.remove('active'); document.getElementById('button-bar-import').classList.add('active'); document.getElementById('content-import').focus(); return false; }); document.getElementById('return-list-add').addEventListener('click', () => { showCookiesForTab(); }); document.getElementById('return-list-import').addEventListener('click', () => { showCookiesForTab(); }); containerCookie.addEventListener('submit', e => { e.preventDefault(); saveCookieForm(e.target); return false; }); document.getElementById('save-create-cookie').addEventListener('click', () => { saveCookieForm(document.querySelector('form')); }); document.getElementById('save-import-cookie').addEventListener('click', e => { let buttonIcon = document.getElementById('save-import-cookie').querySelector('use'); if (buttonIcon.getAttribute("href") !== "../sprites/solid.svg#file-import") { return; } let json = document.querySelector('textarea').value; if (!json) { return; } let cookies; try { cookies = JSON.parse(json); } catch (error) { try { let cookiesTmp = []; // Json failed, let's try netscape format. let lines = json.split('\n'); for (var line of lines) { line = line.trim(); if (!line.length || line[0] == '#') { continue; } const elements = line.split("\t"); if (elements.length != 7) { throw new Error('Invalid netscape format') } cookiesTmp.push({ domain: elements[0], path: elements[2], secure: (elements[3].toLowerCase() === 'true'), expiration: elements[4], name: elements[5], value: elements[6] }); } cookies = cookiesTmp; } catch (error) { sendNotification("Could not parse the value"); buttonIcon.setAttribute("href", "../sprites/solid.svg#times"); setTimeout(() => { buttonIcon.setAttribute("href", "../sprites/solid.svg#file-import"); }, 1500); return; } } if (!isArray(cookies)) { sendNotification("The input is not valid Json/Netscape format"); buttonIcon.setAttribute("href", "../sprites/solid.svg#times"); setTimeout(() => { buttonIcon.setAttribute("href", "../sprites/solid.svg#file-import"); }, 1500); return; } for (let cookie of cookies) { // Make sure we are using the right store ID. This is in case we are importing from a basic store ID and the // current user is using custom containers cookie.storeId = cookieHandler.currentTab.cookieStoreId; if (cookie.sameSite && cookie.sameSite === 'unspecified') { cookie.sameSite = null; } try { cookieHandler.saveCookie(cookie, getCurrentTabUrl(), function (error, cookie) { if (error) { sendNotification(error); } }); } catch (error) { } } sendNotification(`Cookies were imported`); showCookiesForTab(); }); document.querySelector('#advanced-toggle-all input').addEventListener('change', function() { showAllAdvanced = this.checked; browserDetector.getApi().storage.local.set({showAllAdvanced: showAllAdvanced}); showCookiesForTab(); }); notificationElement.addEventListener('animationend', e => { if (notificationElement.classList.contains('fadeInUp')) { return; } triggerNotification(); }); document.getElementById('notification-dismiss').addEventListener('click', e => { hideNotification(); }); initWindow(); showCookiesForTab(); adjustWidthIfSmaller(); if (chrome && chrome.runtime && chrome.runtime.getBrowserInfo) { chrome.runtime.getBrowserInfo(function (info) { const mainVersion = info.version.split('.')[0]; if (mainVersion < 57) { containerCookie.style.height = '600px'; } }); } // Bugfix/hotfix for Chrome 84. Let's remove this once Chrome 90 or later is released if (browserDetector.isChrome()) { document.querySelectorAll('svg').forEach(x => {x.innerHTML = x.innerHTML}); } }); - Assistant
为了实现您提到的功能,需要修改代码逻辑,使得其能够读取文件系统中的
.json文件。然而,在JavaScript中,我们通常不能直接访问本地文件系统。但是,我们可以通过使用HTML的<input type="file">元素或使用Web API来处理用户选择的文件。在此修改后的示例中,我们将使用fetch来模拟从服务器加载文件的场景。以下是一种改造方案。由于无法在客户端JavaScript中直接读取文件系统,这里假设JSON文件可以通过HTTP请求获取。我们使用一个全局变量
currentFileIndex来跟踪当前加载的文件索引,并调整逻辑来在触发点击事件时获取对应的文件。由于原始代码是一部分片段,我们没有上下文,所以以下是修改后的简化示例。我们只修改了与点击
import-cookies按钮相关的部分,并添加了fetchImportFile函数用于导入JSON文件。请注意,这段代码为简化示例,它没有包含原始代码中所有的上下文和辅助函数,并且你可能需要根据实际环境来部署一个可以提供JSON文件的服务器。
// 假设使用的全局变量 let currentFileIndex = 1; // 当前文件索引 document.getElementById('import-cookies').addEventListener('click', () => { if (disableButtons) { return; } // 尝试导入JSON文件 fetchImportFile(currentFileIndex); }); function fetchImportFile(index) { fetch(`./${index}.json`) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); }) .then(cookies => { // 你的处理逻辑,比如导入cookies console.log(`Imported cookies from file ${index}.json`, cookies); currentFileIndex +=1; // 下一次导入下一个文件 }) .catch(error => { console.log(error); // 如果发生错误,比如文件未找到,重置回1.json currentFileIndex = 1; // 你可以选择在这里再次调用 fetchImportFile(1) 来直接重新导入1.json }); } // ...其余代码