为什么此扩展程序无法脱机工作? (包括代码)

时间:2013-05-10 20:59:37

标签: google-chrome google-chrome-extension google-chrome-devtools

我创建了一个扩展程序(请参阅下面的代码),我通过“load unpacked extension”将其添加到Google Chrome中。然后我打开它并在关闭Web服务器之前使用它(它是一个内部服务器)。当我点击使用该应用程序时,它只是在“请求”时挂起。如何强制它仅在离线和本地使用应用程序?

我甚至从计算机上拔掉了以太网(没有wifi),所有其他扩展程序都显示为灰色,但我的颜色仍然保持不变,但是当我点击它时它就抛出了错误:

The app is currently unreachable.

为什么?

manifest.js

{
    "name": "Test App",
    "description": "Something",
    "version": "0.0.0.3",
    "manifest_version": 2,
    "app": {
        "urls": [
            "*://192.168.1.100/chrome/app/"
        ],
        "launch": {
            "web_url": "http://192.168.1.100/chrome/app/"
        }
    },
    "icons": {
        "128": "icon.png"
    },
    "offline_enabled": true,
    "permissions": [
        "unlimitedStorage",
        "notifications"
    ]
}

的index.html

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        loaded
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

问题在于,有两种不同类型的清单文件,您需要两者(谷歌的文档从未提及过这一点!)。

第一个清单是chrome app manifest.json。我在上面的代码中有这个。虽然此清单文件旨在告诉Chrome应用程序所在的文件/网址应该缓存(对于离线可用性),但不会。这由位于托管服务器上的“.manifest”文件处理,并且必须是“text / manifest”类型的文件。以下是让您的Chrome应用脱机工作的步骤:

将mime类型“text / manifest”添加到您的服务器

(在Apache中,您可以将以下内容添加到httpd.conf或.htaccess):

AddType text/cache-manifest .manifest

它不需要命名为“.manifest”,但更容易以这种方式添加mimetype

将链接到index.html的清单设置为指向 清单而不是manifest.json:

<!DOCTYPE html>
<html manifest="mymanifest.manifest">

创建清单文件

CACHE MANIFEST

# These are the files that will be cached and available offline
# Their paths are relative to the manifest file
# These files will all load from AppCache and NOT from the server, BUT
# if you want to update them, simply change the manifest file and the browser
# will redownload them
CACHE:
index.html
icon.png

# This would be files that are not cached but live only online
NETWORK:

# Files that are used if any other files cannot be loaded
# example:
#       someimage.png fallbackimage.png
FALLBACK:

就是这样。用户在线访问应用后,Chrome会正确缓存所有内容,并且可以离线使用。

相关问题