From fbfc10cbc0a595924860161313f76743c1f8cb4a Mon Sep 17 00:00:00 2001 From: andycall Date: Mon, 15 Apr 2024 12:03:06 +0800 Subject: [PATCH] fix: fix html converter --- bin/wbc.js | 5 +++-- demo.html | 8 -------- html_converter.js | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 13 deletions(-) delete mode 100644 demo.html diff --git a/bin/wbc.js b/bin/wbc.js index f39fd31..fbb971c 100755 --- a/bin/wbc.js +++ b/bin/wbc.js @@ -34,9 +34,10 @@ const sourceFileName = source.split('/').slice(-1)[0].split('.')[0]; const sourceCode = fs.readFileSync(source, { encoding: 'utf-8' }); if (options.convertHtml) { + let distPath = path.join(dist, sourceFileName + '.bhtml'); const output = transformInlineScriptToWbc(sourceCode); - fs.writeFileSync(dist, output); - console.log('Quickjs bytecode generated wbc1 at: \n' + dist); + fs.writeFileSync(distPath, output); + console.log('Quickjs bytecode generated at: \n' + distPath); } else { if (type == 'kbc1') { let distPath = path.join(dist, sourceFileName + '.kbc1'); diff --git a/demo.html b/demo.html deleted file mode 100644 index b2a9e19..0000000 --- a/demo.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/html_converter.js b/html_converter.js index cd3ed46..78563cd 100644 --- a/html_converter.js +++ b/html_converter.js @@ -3,6 +3,15 @@ const { JSDOM } = jsdom; const Qjsc = require('./qjsc'); const { Wbc } = require('./wbc'); +const _replaceMap = {}; +let _index = 1; + +function generateReplaceKey(buffer) { + const newIndex = _index++; + _replaceMap[newIndex] = buffer; + return `%@%${newIndex}%@%`; +} + function parseAndConvertHTML(html, version = '1') { let dom = new JSDOM(html); const scripts = dom.window.document.querySelectorAll('script'); @@ -10,15 +19,47 @@ function parseAndConvertHTML(html, version = '1') { [].slice.call(scripts).forEach(script => { const textContent = script.textContent; if (textContent.length == 0) return; + script.type = 'application/vnd.webf.bc1'; + const qjsc = new Qjsc(); const wbc = new Wbc(); const buffer = qjsc.compile(textContent); let wbcBytecode = wbc.generateWbcBytecode(buffer); - let code = wbcBytecode.toString('binary'); - script.textContent = code; + + script.textContent = generateReplaceKey(wbcBytecode); }) - return dom.window.document.documentElement.outerHTML; + const rawHtml = dom.window.document.documentElement.outerHTML; + return replaceIndexWithByteCode(rawHtml); +} + +function replaceIndexWithByteCode(html) { + const byteArray = new Uint8Array(Buffer.from(html)); + const chunks = []; + let start = 0; + + for (let i = 0; i < byteArray.length - 3; i ++) { + if (byteArray[i] == 37 && byteArray[i + 1] == 64 && byteArray[i + 2] == 37) { // %@% + chunks.push(Buffer.from(byteArray.slice(start, i).buffer)); + i += 2; + let index; + let amount = 0; + while(byteArray[i + amount] == 48) { // '0 + amount++ + } + index = amount * 10 + Number(String.fromCharCode(byteArray[i + amount + 1])); + const byteCodeBuffer = _replaceMap[index]; + chunks.push(byteCodeBuffer); + + i += amount + 2; + start = i + 3; + } + } + + chunks.push(byteArray.slice(start)); + + let totalBuffer = Buffer.concat(chunks); + return totalBuffer; }