1

我正在开发一个 chrome 应用程序,它基本上已经完成,但是我正在向我客户的网站添加代码,以确定该应用程序是否安装在 chrome 上。

为了做到这一点,我需要检查一个文件“manifest.json”是否可供应用程序使用,以确定它是否已安装。

我正在使用从此处发布的另一个问题中获得的以下代码:

function detectChromeExtension(extensionId, accesibleResource, callback){
            if (typeof(chrome) !== 'undefined'){
                var xmlHttp = new XMLHttpRequest(),
                    testUrl = 'chrome-extension://' +extensionId +'/' +accesibleResource;
                    xmlHttp.open('HEAD', testUrl, true);
                xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                xmlHttp.timeout = 1000;

                xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && typeof(callback) == 'function') {
                    if (xmlHttp.status == 200) {
                        callback.call(this, true);
                    } else {
                        callback.call(this, false);
                    }
                }
            }        
            xmlHttp.ontimeout = function() {
                if (typeof(callback) == 'function')
                    callback.call(this, false);
                }        
                xmlHttp.send();
            } else {
                if (typeof(callback) == 'function')
                    callback.call(this, false);
            }    
        };

        detectChromeExtension('gcjbmhbihobfgpjmfbooldocijijdpig', 'manifest.json', myCallbackFunction);

        function myCallbackFunction(extensionExists) {
            if (extensionExists){
                console.log('Extension present');
            } else {
                console.log('Extension not present');
            }
        }

通过检查 chrome 控制台,我得到以下输出:

拒绝加载 chrome-extension://gcjbmhbihobfgpjmfbooldocijijdpig/manifest.json。资源必须列在 web_accessible_resources 清单键中,才能被扩展之外的页面加载。

所以为了解决这个问题,我试图将 "web_accessible_resources": ["manifest.json"] 添加到我的 manifest.json 中,但它告诉我这一行不符合语法。

这是完整的 manifest.json:

{
"name": "Watch TBR",
"version": "1.0",
"manifest_version": 2,
"default_locale": "en",
"description": "Easy access to WatchTBR.com",
"icons": { "128": "icon_128.png", "16": "icon_16.png" },
"app": {
"urls": [
  "http://www.watchtbr.com/"
],
"launch": {
  "web_url": "http://www.watchtbr.com/"
}
},
"web_accessible_resources": ["manifest.json"],
"permissions":["http://www.watchtbr.com"]
}

非常感谢您对此的任何帮助。

4

1 回答 1

2

与扩展不同,网站无法访问打包应用程序的资源。如果您同时拥有网站和应用程序,则可以使用 Chrome WebStore 的内联安装功能并测试应用程序是否安装了chrome.app.isInstalled.

目前,该isInstalled方法仅限于托管应用程序,但此错误跟踪打包应用程序的支持。

如果您不拥有该应用程序或只是想共享指向该应用程序(如果已安装)或商店条目(如果未安装)的链接,您可以在https://code上加注星标并跟踪进度.google.com/p/chromium/issues/detail?id=161054

于 2013-04-15T18:05:22.593 回答