// ==UserScript== // @name NAGA Cookie 一键同步 // @namespace https://github.com/Diving-Fish/auto-naga // @version 1.0.0 // @description 将当前 NAGA 登录 Cookie 同步到自行设置的 auto-naga 服务,自动保存设置。 // @match https://naga.dmv.nico/* // @noframes // @run-at document-idle // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // @grant GM_xmlhttpRequest // @grant GM_cookie // @connect www.diving-fish.com // @connect * // @homepageURL https://github.com/Diving-Fish/auto-naga // @downloadURL https://raw.githubusercontent.com/Diving-Fish/auto-naga/main/userscripts/naga-cookie-sync.user.js // @updateURL https://raw.githubusercontent.com/Diving-Fish/auto-naga/main/userscripts/naga-cookie-sync.user.js // ==/UserScript== (() => { 'use strict'; const DEFAULT_REMOTE = 'http://www.diving-fish.com:3165/import_cookies/ui'; const SESSION = 'naga-report-session-id'; const COOKIE_URL = 'https://naga.dmv.nico/naga_report/'; function endpoint(remote) { let url; try { url = new URL(remote.trim()); } catch { throw new Error('Remote 必须是完整的 HTTP 或 HTTPS 地址。'); } if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) { throw new Error('Remote 必须是 HTTP 或 HTTPS 地址,且不能包含用户名或密码。'); } let path = url.pathname.replace(/\/+$/, ''); if (path.endsWith('/import_cookies/ui')) path = path.slice(0, -3); else if (!path.endsWith('/import_cookies')) path += '/import_cookies'; url.pathname = path; url.search = ''; url.hash = ''; return url.href; } async function readCookies() { let cookies = []; if (typeof GM_cookie !== 'undefined' && typeof GM_cookie.list === 'function') { try { cookies = await new Promise((resolve, reject) => { const timer = setTimeout(() => reject(new Error('Cookie 读取超时')), 10000); GM_cookie.list({ url: COOKIE_URL }, (items, error) => { clearTimeout(timer); if (error) reject(new Error(String(error))); else resolve(items || []); }); }); } catch { /* Fall back only for cookies that are visible to the page. */ } } for (const part of document.cookie.split(';')) { const index = part.indexOf('='); if (index < 0) continue; const name = part.slice(0, index).trim(); if (!cookies.some(cookie => cookie.name === name)) { cookies.push({ name, value: part.slice(index + 1), domain: 'naga.dmv.nico', path: '/' }); } } const selected = cookies.filter(cookie => [SESSION, 'csrftoken'].includes(cookie.name) && cookie.value); if (!selected.some(cookie => cookie.name === SESSION)) { throw new Error('未读到登录 Cookie。请先登录 NAGA;若已登录,请使用支持 HttpOnly Cookie 的 Tampermonkey Beta 并检查脚本权限,或使用原来的手动导入页面。'); } if (!selected.some(cookie => cookie.name === 'csrftoken')) { throw new Error('未读到 csrftoken,请打开 NAGA 的解析页面后重试。'); } return selected.map(cookie => ({ name: cookie.name, value: cookie.value, domain: cookie.domain, path: cookie.path || '/', expires: cookie.expirationDate ?? -1 })); } function upload(url, secret, cookies) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: 'POST', url, headers: { 'Content-Type': 'application/json' }, data: JSON.stringify({ secret, cookies }), anonymous: true, timeout: 60000, onload(response) { if (response.status < 200 || response.status >= 300) { reject(new Error(`导入失败:HTTP ${response.status}`)); return; } let body; try { body = JSON.parse(response.responseText); } catch { reject(new Error('服务器未返回 JSON,请检查 Remote 地址。')); return; } if (body?.status !== 200) { const message = body?.message === 'secret error' ? 'Secret 不正确。' : body?.status === 401 ? 'Cookie 无效或已过期,请重新登录 NAGA。' : '服务器拒绝导入,请检查服务端日志。'; reject(new Error(message)); return; } resolve(body); }, onerror: () => reject(new Error('连接失败,请检查 Remote、网络和油猴的跨域访问授权。')), ontimeout: () => reject(new Error('请求超时,请检查服务器状态后重试。')), onabort: () => reject(new Error('请求已取消。')) }); }); } function manualCookies() { return new Promise(resolve => { const dialog = document.createElement('dialog'); dialog.innerHTML = `
`; root.append(dialog); const form = dialog.querySelector('form'); let result = null; form.addEventListener('submit', event => { event.preventDefault(); const session = form.elements.session.value.trim(); const csrf = form.elements.csrf.value.trim(); if (!session || !csrf) return; result = [ { name: SESSION, value: session, domain: 'naga.dmv.nico', path: '/', expires: -1 }, { name: 'csrftoken', value: csrf, domain: 'naga.dmv.nico', path: '/', expires: -1 } ]; dialog.close(); }); dialog.querySelector('#cancel').addEventListener('click', () => dialog.close()); dialog.addEventListener('close', () => { form.reset(); dialog.remove(); resolve(result); }, { once: true }); dialog.showModal(); }); } const host = document.createElement('div'); host.style.cssText = 'position:fixed;right:16px;bottom:16px;z-index:2147483647'; const root = host.attachShadow({ mode: 'closed' }); root.innerHTML = `