/* * v.js * Copyright (C) 2024 veypi * * Distributed under terms of the GPL license. */ import { proxy } from './vyes.js' (function() { 'use strict'; const config = { childList: true, subtree: true }; /** * Represents a book. * @param {Array} mutationsList * @param {MutationObserver} observer */ const callback = function(mutationsList, observer) { for (let mutation of mutationsList) { if (mutation.type === 'childList') { // console.log(mutation) // console.log('A child node has been added or removed.'); } } }; const observer = new MutationObserver(callback); var cacheUrl = {} var pendingRequests = {}; async function fetchFileWithCache(url) { if (cacheUrl[url]) { return Promise.resolve(cacheUrl[url]); } if (pendingRequests[url]) { return pendingRequests[url]; } const promise = fetch(url) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.text(); }) .then(txt => { cacheUrl[url] = txt; return txt; }) .catch(error => { // console.error('Error fetching the file:', error); delete pendingRequests[url]; throw error; }) .finally(() => { delete pendingRequests[url]; }); pendingRequests[url] = promise; return promise; } function extractDoubleBraceContents(str) { const regex = /{{|}}|{for{|{end{/g; let depth = 0; let currentIndex = 0 let results = []; let match; let flag = '' while ((match = regex.exec(str)) !== null) { if (match[0] === '{{') { if (depth === 0) { // 开始一个新的匹配 results.push([flag, str.slice(currentIndex, match.index)]); currentIndex = match.index + 2 flag = 'val' } depth++; } else if (match[0] === '}}') { depth--; if (depth === 0) { results.push([flag, str.slice(currentIndex, match.index)]); currentIndex = match.index + 2 flag = '' } } else if (match[0] === '{for{') { if (depth === 0) { // 开始一个新的匹配 results.push([flag, str.slice(currentIndex, match.index)]); currentIndex = match.index + 5 flag = 'for' } depth++; } else if (match[0] === '{end{') { if (depth === 0) { // 开始一个新的匹配 results.push([flag, str.slice(currentIndex, match.index)]); currentIndex = match.index + 5 flag = 'end' } depth++; } } results.push([flag, str.slice(currentIndex)]); return results; } function extractBodyAndScript(htmlString) { const bodyMatch = htmlString.match(/]*>([\s\S]*)<\/body>/i); const bodyContent = bodyMatch ? bodyMatch[1] : ''; const bodyItems = extractDoubleBraceContents(bodyContent); let scriptMatches = htmlString.match(/]*>([\s\S]*?)<\/script>/ig) || [] let scriptContents = [] let srcReg = /]*src=["']([^"']+)["'][^>]*>/ for (let s of scriptMatches) { let match = srcReg.exec(s) if (match && match.length) { // handle src script // console.log(match[1]) } else { scriptContents.push(s.replace(/]*>|<\/script>/ig, '')) } } // 提取