2014-10-30 98 views
0

我有一個谷歌瀏覽器打包的應用程序,我想根據其清單文件更新應用程序關於頁面的版本號。我的清單文件是像這樣:如何在Chrome應用程序中動態更新版本號?

的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.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> 

如果我不能upda它直接從清單文件,然後有一個angularjs csp兼容的方法來更新單個變量的數量,所以我所要做的就是將腳本鏈接到應用程序中需要更新版本號的任何頁面?

回答

1

您可以直接從清單更新:這是chrome.runtime.getManifest API的用途。

在純JavaScript,你會做這樣的事情:

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

感謝你生命的救星哇我沒想到會是這樣簡單!我會盡可能接受你的回答。 – dragonloverlord 2014-10-30 12:53:17

+1

謝謝!樂意效勞。我開始寫出一個Angular版本,但意識到它對於示例代碼來說要複雜得多,而且實際上取決於你的角度應用程序的其餘部分是什麼樣子,所以我停下了腳步。 :) – 2014-10-30 12:59:39

+0

我編輯你的答案鉻想要一個不同的方式。 – dragonloverlord 2014-10-30 13:53:13

相關問題