Make sure you have the latest version of Flutter.个
To add web support to an existing project created using a previous version of Flutter, run the following command from your project’s top-level directory:
flutter create --platforms web .
Setup
1. Modifying index.html:
导航到web/index.html
文件以删除任何不受支持的脚本:
- 在
<body>
标记内插入以下<script>
标记:
<script src="main.dart.js" type="application/javascript"></script>
Set Extension View Dimensions:
通过修改web/index.html中的起始<html>
标记来定义延伸视图的固定尺寸标注:
<html style="height: 600px; width: 350px">
修改后的index.html
现在应该如下所示:
<!DOCTYPE html>
<html style="height: 600px; width: 300px">
<head>
<meta charset="UTF-8" />
<title>chromextension</title>
</head>
<body>
<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
2. Modifying manifest.json:
用适当的扩展配置替换web/manifest.json
文件的内容,包括名称、描述、版本、内容安全策略、默认弹出窗口和图标:
{
"name": "extension demo",
"description": "demo",
"version": "1.0.0",
"content_security_policy": {
"extension_pages": "script-src 'self' ; object-src 'self'"
},
"action": {
"default_popup": "index.html",
"default_icon": "icons/Icon-192.png"
},
"manifest_version": 3
}
Build the extension
使用HTML渲染器和--csp
标志生成Flutter Web版本以禁用动态代码生成:
flutter build web --web-renderer html --csp
(每次更改代码时,都应该重新构建扩展).
Install and test the extension
现在将安装该扩展,并从Chrome扩展栏访问该扩展: