如何在chrome应用程序中动态更新版本号

How to dynamically update version number in chrome app?

本文关键字:动态 更新 版本号 应用程序 chrome      更新时间:2023-09-26

我有一个谷歌chrome打包的应用程序,我想根据其清单文件更新该应用程序的about页面上的版本号。我的清单文件是这样的:

manifest.json

{
  "manifest_version": 2,
  "name": "Book Writer",
  "version": "2.0.8.0",
  "icons": {
    "16": "images/Book-icon.png",
    "48": "images/Book-icon.png",
    "128": "images/Book-icon.png"
  },
  "app": {
    "background": {
      "scripts": ["main.js"]
    }
  },
  "permissions": [
    "storage",
    "contextMenus"
    ]
}

重要部分是"版本":"2.0.8.0",

我的about页面的结构是这样的:

about.html

<!DOCTYPE html>
  <head>
    <title>about</title>
    <link rel="stylesheet" type="text/css" href="/css/about.css">
  </head>
  <body>
    <h1 class="main-header">book writer v 2.0.8.0</h1>
    <h2 class="sub-header">This game was made by: Dragonloverlord</h2>
    <h2 class="sub-header">LICENSE</h2>
    <div class="license-div">
      <code>
        The MIT License (MIT)<br>
        <br>
        Copyright (c) 2014 dragonloverlord<br>
        <br>
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:<br>
        <br>
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.<br>
        <br>
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.<br>
      </code>
    </div>
  </body>
</html>

如果我不能直接从清单文件更新它,那么是否有一个符合angularjs-csp的方法来更新单个变量的编号,这样我所要做的就是将脚本链接到应用程序中任何需要更新版本号的页面?

您可以直接从清单更新:这就是chrome.runtime.getManifest API的用途。

在纯JavaScript中,您可以执行以下操作:

document.addEventListener('DOMContentLoaded', function () {
  manifest = chrome.runtime.getManifest();
  document.querySelector('h1.main-header').innerHTML = "book writer v " + manifest.version;
});