Chrome 扩展清单 v3 中的 Azure 认知服务 sdk 实现

发布于 2025-01-19 06:17:09 字数 1230 浏览 4 评论 0 原文

我在 Chrome 扩展应用程序中从 MV2 迁移到 MV3 时遇到问题。 使用 MV2,通过 html 文件在 background.js 文件中实现 azure 库一切正常。 但是随着 MV3 和 background.js 文件成为服务工作者,我尝试了一些方法来使我的应用程序正常运行,但没有结果。

根据文档,我将清单文件中的后台部分切换为:

"background": {
        "service_worker": "background.js",
        "type": "module"
    },

我通过 https://github.com/microsoft/cognitive-services-speech-sdk-js

但是当我尝试像这样将其导入background.js时:

import * as SpeechSDK from './distrib/es2015/microsoft.cognitiveservices.speech.sdk.js';
var speechConfig = SpeechSDK.SpeechConfig.fromSubscription(
  "key",
  "region"
);
speechConfig.speechRecognitionLanguage = "fr-FR";
speechConfig.outputFormat = SpeechSDK.OutputFormat.Detailed;

我收到错误“Service Worker 注册失败”和“获取脚本时发生未知错误”。 我根据这篇文章尝试了多种方法,例如使用包装器或 importScripts 函数: chrome 扩展 mv3 - 模块化服务工作线程 js 文件 但它们都不适合我。 您知道如何在 chrome 扩展 MV3 中使用 azure 认知服务吗? 提前致谢 !

I'm having a trouble with the migration from the MV2 to MV3 in my chrome extension application.
With the MV2, everything worked fine implementing the azure library in the background.js file through the html one.
But with the MV3 and the background.js file becoming a service worker i tried several thing to make my app functional with no results.

According to the documentation, i switched background part in the manifest file to :

"background": {
        "service_worker": "background.js",
        "type": "module"
    },

I built the module of the azure cognitive services sdk through https://github.com/microsoft/cognitive-services-speech-sdk-js

But when i try to import it in the background.js like this :

import * as SpeechSDK from './distrib/es2015/microsoft.cognitiveservices.speech.sdk.js';
var speechConfig = SpeechSDK.SpeechConfig.fromSubscription(
  "key",
  "region"
);
speechConfig.speechRecognitionLanguage = "fr-FR";
speechConfig.outputFormat = SpeechSDK.OutputFormat.Detailed;

I get the error "Service worker registration failed" and "An unknown error occurred when fetching the script."
I tried multiples things like using a wrapper or the importScripts function according to this post : chrome extension mv3 - Modularize service worker js file but none of them worked for me.
Do you have any idea how to use the azure cognitive services in a chrome extension MV3 ?
Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2025-01-26 06:17:09
  1. 热权限:

在MV3中需要单独提及热权限。按照下面的代码块提及热权限。

// Manifest V3
"permissions": [
  "tabs",
  "bookmarks"
],
"optional_permissions": [
  "unlimitedStorage"
],
"host_permissions": [
  "http://www.blogger.com/",
  "*://*/*"
],
  1. CSP(内容安全策略):

检查 CSP 标头代码块。

// Manifest V3

"content_security_policy": {
  "extension_pages": "...",
  "sandbox": "..."
}
  1. Action API 统一

background.js 中的 MV2 和 MV3 实现存在细微差别。

// Manifest V3

// manifest.json
{
  "action": { … }
}


// background.js
chrome.action.onClicked.addListener(tab => { … });

为了获得更好的参考,请查看以下文档。

https://developer.chrome.com/docs/extensions/mv3 /简介/mv3-迁移/

  1. Hot Permission:

In MV3 you need to mention the hot permissions separately. Follow the below code block to mention the hot permissions.

// Manifest V3
"permissions": [
  "tabs",
  "bookmarks"
],
"optional_permissions": [
  "unlimitedStorage"
],
"host_permissions": [
  "http://www.blogger.com/",
  "*://*/*"
],
  1. CSP (Content Security Policy):

Check the CSP headers code block.

// Manifest V3

"content_security_policy": {
  "extension_pages": "...",
  "sandbox": "..."
}
  1. Action API Unification:

There is a small different between MV2 and MV3 implementation in background.js

// Manifest V3

// manifest.json
{
  "action": { … }
}


// background.js
chrome.action.onClicked.addListener(tab => { … });

For better reference, check out the below document.

https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文