From 49653d687be501905eca97d8ab39af7c39192095 Mon Sep 17 00:00:00 2001 From: moshangqi <2509678669@qq.com> Date: Thu, 7 Dec 2023 17:53:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20chrome=20beta=20?= =?UTF-8?q?=E6=89=93=E5=8C=85=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- scripts/build-crx.js | 41 ------- scripts/build-zip.sh | 1 + scripts/common.js | 2 +- scripts/zip-crx.sh | 4 - src/background/index.ts | 5 +- .../SuperSideBar/impl/ClipAssistant/index.tsx | 10 +- src/core/transform-dom.ts | 103 +++++++++++++----- webpack.config.js | 16 ++- 9 files changed, 97 insertions(+), 89 deletions(-) delete mode 100644 scripts/build-crx.js delete mode 100755 scripts/zip-crx.sh diff --git a/package.json b/package.json index 94d47af2..8a342ad7 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,9 @@ }, "scripts": { "pack-zip": "sh ./scripts/build-zip.sh", - "pack-crx": "node ./scripts/build-crx.js && sh ./scripts/zip-crx.sh", "bundle": "NODE_ENV=production webpack --mode=production", - "build": "npm run bundle && npm run pack-zip && npm run pack-crx", + "bundle:beta": "NODE_ENV=production BETA=beta webpack --mode=production", + "build": "npm run bundle && npm run pack-zip", "clean:dist": "sh ./scripts/clean.sh", "postinstall": "npm run clean:dist", "contributor": "git-contributor", diff --git a/scripts/build-crx.js b/scripts/build-crx.js deleted file mode 100644 index e86009f0..00000000 --- a/scripts/build-crx.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const CRX = require('crx'); -const pkg = require('../package.json'); -const { distFolder, cdnPrefix } = require('./common'); - -async function buildCrxFromZip() { - if (!process.env.CHROME_KEY_PEM) { - console.warn('No CHROME_KEY_PEM found, skip building crx.'); - return; - } - - const crx = new CRX({ - privateKey: decodeURIComponent(process.env.CHROME_KEY_PEM), - // use this field in update.xml - codebase: `${cdnPrefix}/${pkg.version}.crx`, - }); - - // 将 update_url 写入 manifest.json 中 - // chrome 商店版本不能写入 update_url - // 仅作为离线版本分发 - const manifestJSON = require(path.resolve(distFolder, 'manifest.json')); - manifestJSON.update_url = `${cdnPrefix}/updates.xml`; - fs.writeFileSync( - path.resolve(distFolder, 'manifest.json'), - JSON.stringify(manifestJSON, null, 2), - 'utf-8' - ); - - return crx.load(distFolder) - .then(crx => crx.pack()) - .then(crxBuffer => { - fs.writeFileSync(path.resolve(__dirname, '..', pkg.version + '.crx'), crxBuffer); - const xmlBuffer = crx.generateUpdateXML(); - fs.writeFileSync(path.resolve(__dirname, '../updates.xml'), xmlBuffer); - }); -} - -buildCrxFromZip(); diff --git a/scripts/build-zip.sh b/scripts/build-zip.sh index 56b1543a..65c654d4 100755 --- a/scripts/build-zip.sh +++ b/scripts/build-zip.sh @@ -1,3 +1,4 @@ CRUEENT_VERSION=`node -e "console.log(require('./package').version)"` zip -r $CRUEENT_VERSION.zip ./dist/$CRUEENT_VERSION +zip -r $CRUEENT_VERSION-beta.zip ./dist/$CRUEENT_VERSION-beta diff --git a/scripts/common.js b/scripts/common.js index 67874b70..e1381deb 100644 --- a/scripts/common.js +++ b/scripts/common.js @@ -3,7 +3,7 @@ const path = require('path'); const pkg = require('../package.json'); -const distFolder = path.resolve(__dirname, '..', 'dist', pkg.version); +const distFolder = path.resolve(__dirname, '..', 'dist', process.env.BETA === 'beta' ? `${pkg.version}-beta` : pkg.version); const cdnPrefix = 'https://app.nlark.com/yuque-chrome-extension'; const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); diff --git a/scripts/zip-crx.sh b/scripts/zip-crx.sh deleted file mode 100755 index d6349302..00000000 --- a/scripts/zip-crx.sh +++ /dev/null @@ -1,4 +0,0 @@ -CRUEENT_VERSION=`node -e "console.log(require('./package').version)"` - -# .crx 默认下载会被拦截,需要压缩下载后再解压 -zip -r $CRUEENT_VERSION.crx.zip updates.xml $CRUEENT_VERSION.crx diff --git a/src/background/index.ts b/src/background/index.ts index f579f6b7..e1daec21 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -18,6 +18,9 @@ listenShortcut(); chrome.runtime.onInstalled.addListener(async details => { console.log('-- runtime installed'); + createContextMenu(); + updateDynamicRules(); + if (details.reason === 'install') { chrome.tabs.create({ url: LinkHelper.introduceExtension, @@ -64,8 +67,6 @@ chrome.runtime.onInstalled.addListener(async details => { ); chrome.runtime.reload(); } - createContextMenu(); - updateDynamicRules(); }); chrome.runtime.setUninstallURL(LinkHelper.unInstallFeedback); diff --git a/src/components/SuperSideBar/impl/ClipAssistant/index.tsx b/src/components/SuperSideBar/impl/ClipAssistant/index.tsx index 105e75c0..054bd8d5 100644 --- a/src/components/SuperSideBar/impl/ClipAssistant/index.tsx +++ b/src/components/SuperSideBar/impl/ClipAssistant/index.tsx @@ -151,12 +151,10 @@ function ClipContent() { const onUploadImage = useCallback(async (params: { data: string }) => { const file = await transformUrlToFile(params.data); - const res = await Promise.all( - [ - backgroundBridge.request.upload.attach(params.data), - ocrManager.startOCR('file', file), - ].map(p => p.catch(e => e)), - ); + const res = await Promise.all([ + backgroundBridge.request.upload.attach(params.data), + ocrManager.startOCR('file', file), + ]); return { ...(res[0] || {}), ocrLocations: res[1], diff --git a/src/core/transform-dom.ts b/src/core/transform-dom.ts index 5b213563..57078223 100644 --- a/src/core/transform-dom.ts +++ b/src/core/transform-dom.ts @@ -28,29 +28,53 @@ function hexoCodeBlock(cloneNode: Element) { function commonCodeBlock(node: Element) { const preElements = node.querySelectorAll('pre'); + /** + * 查询所有 pre 节点 + * 并将 pre 节点下所有的 code 节点融合成一个新的 code 节点 + *
+   *  1
+   *  
    2
+ *
+ * 转化后 + *
+   *  
+   *    
1
+ *
2
+ *
+ *
+ */ preElements.forEach(pre => { - const codeElement = pre.querySelector('code'); - if (codeElement) { - const childNodes = pre.childNodes; - const needRemoveNodes: ChildNode[] = []; - const needMergeNodes: ChildNode[] = []; - childNodes.forEach(item => { - if ((item as Element)?.tagName === 'CODE' && item !== codeElement) { - needMergeNodes.push(item); - } - if (item !== codeElement) { - needRemoveNodes.push(item); - } - }); - // 将非 code 移除掉 - needRemoveNodes.forEach(item => { + const codeElementArray = pre.querySelectorAll('code'); + const cleanCode: ChildNode[] = []; + for (const codeElement of codeElementArray) { + if (codeElement) { + const childNodes = pre.childNodes; + const needRemoveNodes: ChildNode[] = []; + const needMergeNodes: ChildNode[] = []; + childNodes.forEach(item => { + if ((item as Element)?.tagName === 'CODE' && item !== codeElement) { + needMergeNodes.push(item); + } + if (item !== codeElement) { + needRemoveNodes.push(item); + } + }); + const div = document.createElement('div'); + codeElement.childNodes.forEach(item => { + div.appendChild(item); + }); + cleanCode.push(div); + } + // 移除掉所有的子节点 + pre.childNodes.forEach(item => { pre.removeChild(item); }); - // 将多 code 合成一个 dom - needMergeNodes.forEach(item => { - codeElement.appendChild(document.createElement('br')); - item.childNodes.forEach(codeChild => codeElement.appendChild(codeChild)); + const code = document.createElement('code'); + cleanCode.forEach(item => code.appendChild(item)); + pre.childNodes.forEach(item => { + pre.removeChild(item); }); + pre.appendChild(code); } }); } @@ -96,7 +120,10 @@ function findYuqueChildId(element: Element | null) { } function isYuqueContent(element: Element) { - if (element.closest('.ne-viewer-body') || document.querySelector('.ne-viewer-body')) { + if ( + element.closest('.ne-viewer-body') || + document.querySelector('.ne-viewer-body') + ) { return true; } return false; @@ -130,7 +157,9 @@ async function transformYuqueContent(element: Element) { }, 3000); await new Promise(resolve1 => { - let script = document.querySelector('#yuque-content-transform-script') as HTMLScriptElement; + let script = document.querySelector( + '#yuque-content-transform-script', + ) as HTMLScriptElement; if (script) { return resolve1(true); } @@ -155,7 +184,9 @@ async function transformYuqueContent(element: Element) { ids.push(id); } } else if (element.querySelector('.ne-viewer-body')) { - const childIds = findYuqueChildId(element.querySelector('.ne-viewer-body')); + const childIds = findYuqueChildId( + element.querySelector('.ne-viewer-body'), + ); ids = ids.concat(childIds); } @@ -177,7 +208,11 @@ interface IOriginAndCloneDomItem { clone: Element; } -function generateOriginAndCloneDomArray(cloneElement: Element, originElement: Element, name: keyof HTMLElementTagNameMap): Array { +function generateOriginAndCloneDomArray( + cloneElement: Element, + originElement: Element, + name: keyof HTMLElementTagNameMap, +): Array { const originDoms = originElement.querySelectorAll(name); const cloneDoms = cloneElement.querySelectorAll(name); const result: Array = []; @@ -202,7 +237,11 @@ function generateOriginAndCloneDomArray(cloneElement: Element, originElement: El } async function transformVideoToImage(element: Element, originDom: Element) { - const videoMapArray = generateOriginAndCloneDomArray(element, originDom, 'video'); + const videoMapArray = generateOriginAndCloneDomArray( + element, + originDom, + 'video', + ); for (const videoMap of videoMapArray) { const rect = videoMap.origin.getBoundingClientRect(); @@ -224,7 +263,11 @@ async function transformVideoToImage(element: Element, originDom: Element) { } function transformCanvasToImage(element: Element, originDom: Element) { - const canvasMapArray = generateOriginAndCloneDomArray(element, originDom, 'canvas'); + const canvasMapArray = generateOriginAndCloneDomArray( + element, + originDom, + 'canvas', + ); for (const canvasMap of canvasMapArray) { const imageElement = document.createElement('img'); @@ -255,12 +298,18 @@ export async function transformDOM(domArray: Element[]) { clonedDOMArray.push(div); } - for (let clonedDOMIndex = 0; clonedDOMIndex < clonedDOMArray.length; clonedDOMIndex++) { + for ( + let clonedDOMIndex = 0; + clonedDOMIndex < clonedDOMArray.length; + clonedDOMIndex++ + ) { let clonedDOM = clonedDOMArray[clonedDOMIndex]; if (isYuqueContent(clonedDOM)) { try { - clonedDOMArray[clonedDOMIndex] = (await transformYuqueContent(clonedDOM)) as Element; + clonedDOMArray[clonedDOMIndex] = (await transformYuqueContent( + clonedDOM, + )) as Element; yuqueDOMIndex.push(clonedDOMIndex); continue; } catch (error) { diff --git a/webpack.config.js b/webpack.config.js index 561c0129..117d00ae 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -10,6 +10,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); const TerserPlugin = require('terser-webpack-plugin'); const WebpackNotifierPlugin = require('webpack-notifier'); const WebpackLogPlugin = require('./scripts/log-plugin'); +const isBeta = process.env.BETA === 'beta'; const { presetEditor, @@ -62,16 +63,20 @@ const plugins = [ from: path.join(srcPath, 'manifest.json'), transform(content) { const origin = JSON.parse(content.toString()); - const value = JSON.stringify(Object.assign({ + const value = Object.assign({ version: pkg.version, name: pkg.description, - }, origin), null, 2); - return Buffer.from(value); + }, origin); + if (isBeta) { + value.name = `${value.name} BETA`; + value.description = `${value.description} (THIS EXTENSION IS FOR BETA TESTING)` + } + return Buffer.from(JSON.stringify(value, null, 2)); }, }, { from: path.join(srcPath, 'background/background-wrapper.js'), - to: path.join(distPath, pkg.version, 'background-wrapper.js'), + to: path.join(distPath, isBeta ? `${pkg.version}-beta` : pkg.version, 'background-wrapper.js'), }, ], }), @@ -202,7 +207,7 @@ const options = { stats: 'errors-only', entry, output: { - path: path.join(__dirname, 'dist', pkg.version), + path: path.join(__dirname, 'dist', isBeta ? `${pkg.version}-beta` : pkg.version), filename: '[name].js', }, module: { @@ -250,7 +255,6 @@ const options = { }, }; - module.exports = async () => { await presetEditor(); return options;