1

包含 manifest.json 的标准方式:

<link rel="manifest" href="manifest.json">

有没有办法将 manifest.json 的内容直接包含在 HTML 文档中?

(这样做的原因是为了避免 HTTP 请求,并根据仅在我的 html 模板中直接可用的标签自动生成文件)

更准确地说,这是我正在尝试做的一个非工作示例,只是为了这个想法:

<link rel="manifest" content="{"name": "Web Starter Kit", "other options": "directly here"}">

4

2 回答 2

2

首先,不要使用相同的引号。HTML 认为是

content=
"{"
name
": "
Web Starter Kit
", "
other options
": "
directly here
"}"

加号链接不能有内容属性,所以使用<script type="application/json">

接下来,要使用点表示法,您应该排除空格,manifest.other options以免混淆程序。

现在阅读 JSON 使用

var manifest= document.getElementById('myJSON').innerHTML; //sets manifest to the text in #myJSON
manifest= JSON.parse(manifest) //Converts it into JSON

以下是您想要的工作示例。

    //var manifest= JSON.parse(document.getElementById('myJSON').innerHTML); /*Shortend of 2&3*/
var manifest= document.getElementById('myJSON').innerHTML; //sets manifest to the text in #myJSON
manifest= JSON.parse(manifest) //Converts it into JSON
document.getElementById('test').innerHTML= manifest.name+ '<br/>'+ manifest.otherOptions; //Displays it
console.log('manifest')
console.log(manifest);
<head>
<script type="application/json" id="myJSON">
  {"name":"Web Starter Kit", "otherOptions":"directly here"}
</script>
</head>
<body>
<p id="test"></p>
</body>

是的,您可以<script>用于 javascript,也可以使用全局变量(变量不在函数中)

于 2017-05-24T14:20:30.833 回答
0

如何使用标准 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 浏览器。

于 2021-06-08T11:09:56.237 回答