如何使用标准 JavaScript 内联动态生成的 manifest.json 文档
我在 html 标头中包含了以下链接 rel ,但没有有效的 href 属性:
链接 rel="manifest" id="my-manifest-placeholder"
(另请参阅“如何使用 Javascript 动态设置您的 Web 应用程序清单” https://medium.com/@alshakero/how-to-setup-your-web-app-manifest-dynamically-using-javascript-f7fbee899a61不幸的是不适合我!)
(或“Inline Web App Manifest”,Inline the Web App Manifest?这极大地启发了我的解决方案)
后来,我运行了以下 javascript:
thisHostUrl = "http://localhost/";
// json 清单,转义为字符串
var myManifest = '{\"short_name\": \"ungravel\",\"name\": \"Ungravel:%20Cofounder%20GroupWallet\",\"icons\": [{\"src\": \"' + thisHostUrl + 'icon192x192.png\",\"type\": \"image\/png\",\"sizes\": \"192x192\",\"purpose\": \"any%20maskable\"},{\"src\": \"' + thisHostUrl + 'icon512x512.png\",\"type\": \"image\/png\",\"sizes\": \"512x512\",\"purpose\": \"any%20maskable\"}],\"start_url\": \"' + thisHostUrl + '\",\"background_color\": \"rgb(225,230,233)\",\"display_override\": [\"window-control-overlay\", \"standalone\"],\"display\": \"fullscreen\",\"orientation\": \"landscape-primary\",\"scope\": \"' + thisHostUrl + '\",\"theme_color\": \"rgb(179,231,253)\",\"shortcuts\": [{\"name\": \"Ungravel:%20Cofounder%20GroupWallet\",\"short_name\": \"ungravel\",\"description\": \"Create%20co-founder%20group%20with%20shares%20and%20GroupWallet\",\"url\": \"' + thisHostUrl + '\",\"icons\": [{ \"src\": \"' + thisHostUrl + 'icon64x64.png\", \"sizes\": \"64x64\", \"type\": \"image\/png\" }, { \"src\": \"' + thisHostUrl + 'icon192x192.png\", \"sizes\": \"192x192\", \"type\": \"image\/png\"}]}],\"description\": \"Co-found%20a%20distributed%20group%20on%20ethereum%20blockchain,%20based%20on%20ERC20-compatible%20GroupToken%20shares%20and%20NameRegistry%20NFTs%20(ENS)\",\"screenshots\": [{\"src\": \"' + thisHostUrl + 'screenshot1.jpg\",\"type\": \"image\/jpg\",\"sizes\": \"842x790\"},{\"src\": \"' + thisHostUrl + 'screenshot2.jpg\",\"type\": \"image\/jpg\",\"sizes\": \"1144x630\"}]}';
const stringManifest = 'data:application/manifest+json,'+myManifest;
document.querySelector('#my-manifest-placeholder').setAttribute('href', stringManifest);
不要忘记转义空格和特殊字符,例如 / 和 ""。
https://www.tutorialspoint.com/json_simple/json_simple_escape_characters.htm
请注意背景和主题颜色的颜色代码:rgb(179,231,253) 不要使用十六进制颜色或转义 #-special 字符。
我尝试了 URL.createObjectURL(xxxxx),但动态 href 很满意:
data:application/manifest+json + 动态生成的清单,在我的例子中是 myManifest
Google 版本 91.0.4472.77 (Offizieller Build) (x86_64) 从我的源加载清单,无需任何额外的网络访问,从而节省了我的单页 PWA 的网络请求。有点难看,但它的工作原理。
由于清单格式尚未最终确定且仍在开发中,我认为属性可能会发生变化,并且此解决方案可能不适用于其他 Google Chrome 浏览器。