程序员老王 发布的文章

// 引入 html2canvas
import html2canvas from 'html2canvas';


async function captureElement(elementSelector, image_name = 'screenshot_from_html2canvas') {
    const element = document.querySelector(elementSelector);
    // 获取元素的实际渲染尺寸
    const rect = element.getBoundingClientRect();
    const width = rect.width;
    const height = rect.height;

    // 创建临时容器(避免截取时元素被缩放影响布局)
    const container = document.createElement('div');
    Object.assign(container.style, {
        position: 'fixed',
        left: '-9999px',      // 隐藏到屏幕外
        width: `${width}px`,  // 固定容器尺寸
        height: `${height}px`
    });
    document.body.appendChild(container);

    // 克隆元素到临时容器(保持样式)
    const clone = element.cloneNode(true);
    container.appendChild(clone);

    // width和height是为了和屏幕上显示的大小一致,不加也可以
    const canvas = await html2canvas(element, {
        useCORS: true,       // 允许跨域资源
        logging: false,      // 关闭日志
        scale: 1,            // 必须设为 1,否则尺寸会缩放
        width: width,        // 显式设置宽高
        height: height,
        backgroundColor: null // 透明背景
    });

    // 转换为图片 URL
    const url = canvas.toDataURL('image/png', 1.0);
    const data = {
        url: url,
        filename: `${image_name}.png`
    }
    // downloadImg(data);
    postMessage({type: "download_image", data})
    return url
}

读取剪切板需要有用户交互,比如点击,第一次需要申请权限

<a-button :disabled="Object.keys(harFiles.valueOf()).length===0" @click="readClipboardText">add record from
          clipboard
        </a-button>
const readClipboardText = async () => {
  try {
    // 请求剪切板读取权限
    const permission = await navigator.permissions.query({name: 'clipboard-read'});

    if (permission.state === 'granted' || permission.state === 'prompt') {
      // 读取文本内容
      let text = await navigator.clipboard.readText()
      console.log(text)
      
    } else {
      alert('请允许剪切板访问权限!');
    }
  } catch (error) {
    console.error('读取失败:', error);
    alert('无法读取剪切板内容,请确保浏览器支持或已授予权限');
  }
};

[2025-02-10 13:40:41] [ERROR] [unknown:0] [0] got error:TimeoutError: elementHandle.hover: Timeout 2000ms exceeded.
Call log:

  • attempting hover action
    2 × waiting for element to be visible and stable

    • element is visible and stable
    • scrolling into view if needed
    • done scrolling
    • performing hover action
    • from
      subtree intercepts pointer events
    • retrying hover action
    • waiting 20ms
    • × waiting for element to be visible and stable

      • element is visible and stable
      • scrolling into view if needed
      • done scrolling
      • performing hover action
      • from
        subtree intercepts pointer events
    • retrying hover action

      • waiting 100ms
    • × waiting for element to be visible and stable

      • element is visible and stable
      • scrolling into view if needed
      • done scrolling
      • performing hover action
      • from
        subtree intercepts pointer events
    • retrying hover action

      • waiting 500ms

<div id="slideBg"> 拦截了指针事件(intercepts pointer events)

安装

pnpm install json-editor-vue

使用

<template>

<JsonEditorVue
        v-model="json"
        mode="text"
        :mainMenuBar="false"
        :statusBar="true"
        :stringified="false"
    />
</template>

<script lang="ts" setup>

import JsonEditorVue from 'json-editor-vue'
const json = {"name":"张三"}
</script>

问题

开发和打包成二进制都没问题,打包mac独立app onefile不显示窗口

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon("icons/icon.png"))  # 确保 "icon.png" 路径正确,可以是绝对路径或相对路径
    from PyQt5.QtCore import Qt
    QApplication.setAttribute(Qt.AA_DontUseNativeMenuBar, True)


    # 设置全局日志回调(使得代理模块调用 log() 时同时能写入 UI)
    def gui_log(msg):
        window.log(msg)
    LOG_CALLBACK = gui_log

    window = MainWindow()
    window.setWindowTitle("代理")

    window.resize(600, 600)
    window.show()
    sys.exit(app.exec_())

开发,打包二进制没问题,打包mac应用不显示窗口
注释掉python window.resize(600, 600)就好了