这是用户在 2025-7-1 10:19 为 https://wxt.dev/guide/essentials/remote-code.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Skip to content  跳转到内容

Remote Code   远程代码

WXT will automatically download and bundle imports with the url: prefix so the extension does not depend on remote code, a requirement from Google for MV3.
WXT 将自动下载并捆绑以 url: 前缀导入的文件,这样扩展就不会依赖远程代码,这是 Google 对 MV3 的要求。

Google Analytics

For example, you can import Google Analytics:
例如,您可以导入 Google Analytics:

ts
// utils/google-analytics.ts
import 'url:https://www.googletagmanager.com/gtag/js?id=G-XXXXXX';

window.dataLayer = window.dataLayer || [];
// NOTE: This line is different from Google's documentation
window.gtag = function () {
  dataLayer.push(arguments);
};
gtag('js', new Date());
gtag('config', 'G-XXXXXX');

Then you can import this in your HTML files to enable Google Analytics:
然后您可以在 HTML 文件中导入它以启用 Google Analytics:

ts
// popup/main.ts
import '~/utils/google-analytics';

gtag('event', 'event_name', {
  key: 'value',
});