From 79585da86edf0e742fe204c538eaff4cb799b28b Mon Sep 17 00:00:00 2001 From: fzk <458813868@qq.com> Date: Tue, 17 Sep 2024 23:17:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E9=A1=B5=E9=9D=A2=E6=88=AA=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- background/messages/screenshot.ts | 20 ++++++++++++ component/options/config.tsx | 16 ++++++++++ contents/custom.tsx | 52 ++++++++++++++++++++++++------- locales/en/messages.json | 3 ++ locales/zh_CN/messages.json | 3 ++ package.json | 5 ++- utils/drawImages.ts | 42 +++++++++++++++++++++++++ 7 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 background/messages/screenshot.ts create mode 100644 utils/drawImages.ts diff --git a/background/messages/screenshot.ts b/background/messages/screenshot.ts new file mode 100644 index 0000000..62da3ec --- /dev/null +++ b/background/messages/screenshot.ts @@ -0,0 +1,20 @@ +import type { PlasmoMessaging } from "@plasmohq/messaging" + +const handler: PlasmoMessaging.MessageHandler = async (req, res) => { + const tabs = await chrome.tabs.query({ currentWindow: true, active: true }) + const tab = tabs[0] + chrome.tabs.captureVisibleTab( + tab.windowId, + { + format: "png" + }, + (dataUrl) => { + console.log(dataUrl) + res.send({ + dataUrl: dataUrl + }) + } + ) +} + +export default handler diff --git a/component/options/config.tsx b/component/options/config.tsx index 4ffc660..8a98d7c 100644 --- a/component/options/config.tsx +++ b/component/options/config.tsx @@ -1,3 +1,6 @@ +import { DownloadOutlined, StarTwoTone } from "@ant-design/icons" + +import { sendToBackground, sendToContentScript } from "@plasmohq/messaging" import { useStorage } from "@plasmohq/storage/hook" import { i18n } from "~tools" @@ -7,6 +10,12 @@ export default function Config() { v === undefined ? true : v ) + async function downloadImg() { + sendToContentScript({ + name: "app-full-page-screenshot" + }) + } + return (
{i18n("AppConfig")} @@ -22,6 +31,13 @@ export default function Config() { /> +
+ + + {i18n("fullPageScreenshot")} + + +
) } diff --git a/contents/custom.tsx b/contents/custom.tsx index 49c1e90..db84240 100644 --- a/contents/custom.tsx +++ b/contents/custom.tsx @@ -1,10 +1,12 @@ -import { Modal, message } from "antd" +import { message, Modal } from "antd" import { useEffect, useState } from "react" +import { sendToBackground } from "@plasmohq/messaging" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { addCss, saveHtml, saveMarkdown, saveTxt, setIcon } from "~tools" +import DrawImages from "~utils/drawImages" import Dom2Pdf from "~utils/html2Pdf" import Turndown from "~utils/turndown" @@ -46,22 +48,46 @@ export default function Custom() { if (req.name == "custom-downloadHtml") { isSelect = true isDownloadType = "html" - message.info('请在页面选择要下载区域!') + message.info("请在页面选择要下载区域!") } if (req.name == "custom-downloadMarkdown") { isSelect = true isDownloadType = "markdown" - message.info('请在页面选择要下载区域!') + message.info("请在页面选择要下载区域!") } if (req.name == "custom-downloadPdf") { isSelect = true isDownloadType = "pdf" - message.info('请在页面选择要下载区域!') + message.info("请在页面选择要下载区域!") } if (req.name == "custom-downloadImg") { isSelect = true isDownloadType = "img" - message.info('请在页面选择要下载区域!') + message.info("请在页面选择要下载区域!") + } + if (req.name == "app-full-page-screenshot") { + const { scrollHeight, clientHeight } = document.documentElement + const devicePixelRatio = window.devicePixelRatio || 1 + + let capturedHeight = 0 + let capturedImages = [] + + const captureAndScroll = async () => { + const scrollAmount = clientHeight * devicePixelRatio + const res = await sendToBackground({ name: "screenshot" }) + const dataUrl = res.dataUrl + + capturedHeight += scrollAmount + if (capturedHeight < scrollHeight * devicePixelRatio) { + capturedImages.push(dataUrl) + window.scrollTo(0, capturedHeight) + setTimeout(captureAndScroll, 2000) // Adjust the delay as needed + } else { + DrawImages(capturedImages, articleTitle) + } + } + + captureAndScroll() } }) @@ -118,14 +144,16 @@ export default function Custom() { isSelect = false Modal.confirm({ title: "提示", - content:( - <> -
是否保存?
-
此功能限时免费免登录,预计10月份以后需要注册,后续可能收费...
- + content: ( + <> +
是否保存?
+
+ 此功能限时免费免登录,预计10月份以后需要注册,后续可能收费... +
+ ), - okText: '确认', - cancelText: '取消', + okText: "确认", + cancelText: "取消", onOk: () => { if (isDownloadType == "html") { downloadHtml(currentDom) diff --git a/locales/en/messages.json b/locales/en/messages.json index 1b18702..d81e359 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -154,5 +154,8 @@ }, "weixinConfig": { "message": "Wechat Config" + }, + "fullPageScreenshot": { + "message": "Full Page Screenshot" } } \ No newline at end of file diff --git a/locales/zh_CN/messages.json b/locales/zh_CN/messages.json index f93318b..ec92f99 100644 --- a/locales/zh_CN/messages.json +++ b/locales/zh_CN/messages.json @@ -154,5 +154,8 @@ }, "weixinConfig": { "message": "微信设置" + }, + "fullPageScreenshot": { + "message": "全页面截图" } } \ No newline at end of file diff --git a/package.json b/package.json index 0792639..82c3465 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-box", "displayName": "__MSG_extensionName__", - "version": "0.6.10", + "version": "0.6.12", "description": "__MSG_extensionDescription__", "author": "027xiguapi. <458813868@qq.com>", "scripts": { @@ -60,6 +60,9 @@ "id": "$PLASMO_PUBLIC_FIREFOX_EXT_ID" } }, + "permissions": [ + "activeTab" + ], "host_permissions": [ "https://*/*" ], diff --git a/utils/drawImages.ts b/utils/drawImages.ts new file mode 100644 index 0000000..ab29ab1 --- /dev/null +++ b/utils/drawImages.ts @@ -0,0 +1,42 @@ +import dayjs from "dayjs" +import { saveAs } from "file-saver" + +export default function DrawImages(images, filename) { + if (images.length === 0) { + console.error("No images captured.") + return + } + + const canvas = document.createElement("canvas") + const context = canvas.getContext("2d") + const firstImage = new Image() + firstImage.onload = () => { + canvas.width = firstImage.width + canvas.height = images.length * firstImage.height + + let imagesLoaded = 0 + + const drawImageOnCanvas = (image, index) => { + context.drawImage(image, 0, index * firstImage.height) + imagesLoaded++ + + if (imagesLoaded === images.length) { + filename = filename || "CodeBox-page" + canvas.toBlob(function (blob) { + saveAs( + blob, + `${filename}-${dayjs().format("YYYY-MM-DD HH:mm:ss")}.png` + ) + }) + } + } + + images.forEach((dataUrl, index) => { + const image = new Image() + image.onload = () => drawImageOnCanvas(image, index) + image.src = dataUrl + }) + } + + firstImage.src = images[0] +}