You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展中Tesseract.js无法正常运行的问题求助

Chrome扩展中Tesseract.js无法正常运行的问题求助

我正在开发一款Chrome扩展,想用Tesseract.js实现图片文字识别的功能,但遇到了个头疼的问题:Tesseract.js默认会在运行时从CDN加载worker.min.js,可Chrome的内容安全策略(CSP)禁止加载外部脚本。我把自己的manifest.json贴出来,也说说试过的几种解决方法,但全都没起作用,希望大家能帮忙看看。

我试过的几种方案

  • 尝试改用本地的worker脚本,代码如下,但依然不行——因为本地脚本是通过extension://app_id/worker.min.js路径加载的,还是触发了CSP限制
  • 调整了extension_pages里的CSP规则,试着添加blob:和'wasm-unsafe-eval'这两个选项,结果问题还是没解决

尝试的代码示例

// 尝试用本地脚本初始化Worker
const worker = Tesseract.createWorker({
  workerPath: chrome.runtime.getURL('assets/worker.min.js'),
  corePath: chrome.runtime.getURL('assets/tesseract-core.wasm.js'),
  langPath: chrome.runtime.getURL('assets/lang/')
});

// 尝试用Blob URL加载本地Worker脚本
const worker = Tesseract.createWorker({
  workerPath: blobURL,
  corePath: chrome.runtime.getURL('tesseract-core.wasm.js'),
  langPath: chrome.runtime.getURL('lang/')
});

我的manifest.json配置

{
  "manifest_version": 3,
  "name": "Test Study",
  "version": "1.0",
  "description": "Generate quizzes and flashcards from selected text and screenshots.",
  "permissions": [
    "contextMenus",
    "tabs",
    "activeTab",
    "scripting",
    "storage",
    "identity"
  ],
  "oauth2": {
    "client_id": "************************.apps.googleusercontent.com",
    "scopes": [
      "email",
      "profile",
      "https://www.googleapis.com/auth/userinfo.email",
      "https://www.googleapis.com/auth/userinfo.profile"
    ]
  },
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": [
    "http://localhost:4200/*",
    "https://app.testudy.io/*",
    "https://app2.testudy.io/*",
    "<all_urls>"
  ],
  "action": {
    "default_popup": "index.html",
    "default_icon": {
      "16": "logo_16.png",
      "24": "logo_24.png",
      "32": "logo_32.png"
    }
  },
  "icons": {
    "16": "logo_16.png",
    "48": "logo.png",
    "128": "logo.png"
  },
  "content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self';"
  },
  "content_scripts": [
    {
      "matches": [
        "http://localhost:4200/*",
        "http://localhost/*",
        "https://app.testudy.io/*",
        "https://app2.testudy.io/*"
      ],
      "js": ["content.js"],
      "run_at": "document_end"
    }
  ],
  "web_accessible_resources": [
    {
      "resources": ["worker.min.js"],
      "matches": ["<all_urls>"]
    }
  ]
}

备注:内容来源于stack exchange,提问作者Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:52:57