javascript - 添加到主屏幕到图像
问题描述
我的 PHP 页面中有一个下载按钮。单击该按钮后,将下载图像。但我的老板需要下载图像,如应用程序图标。如果有人下载图像,则应保存一个图标,如果单击该图标,则应显示下载的图像。
我通过“添加到主屏幕”进行了尝试。它正在重新加载页面。
index.html 是:
<!DOCTYPE html>
<html>
<head>
<title>Template One</title>
<link rel="icon" href="icon512.png" type="image/png" sizes="16x16">
<link rel="manifest" href="manifest.json">
</head>
<body>
<div class="mainContainer template1">
hello
</div>
</body>
<!-- Latest compiled and minified JavaScript -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="crossorigin="anonymous"></script>
<script>
if ('serviceWorker' in navigator) {
console.log("Will the service worker register?");
navigator.serviceWorker.register('service-worker.js')
.then(function(reg){
console.log("Yes, it did.");
}).catch(function(err) {
console.log("No it didn't. This happened:", err)
});
}
</script>
</html>
service-worker.js 是:
self.addEventListener('fetch', function(event) {});
manifest.json 是:
{
"short_name": "Test App",
"name": "Web Test App",
"icons": [
{
"src": "icon192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "icon512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "http://localhost/add_to_home_screen3/",
"background_color": "#3367D6",
"display": "standalone",
"theme_color": "#3367D6"
}
它没有显示任何添加到主屏幕按钮。
在控制台中:
Will the service worker register?
(index):24 Yes, it did.
我在桌面 chrome localhost 中测试了我的代码。
我有一些疑问:
1-为什么它不起作用。我这边的代码有什么问题,还是只能在移动浏览器上运行?
2- 我可以在 manifest.json 的 start_url 中给出我的图像名称 url 。
3-上述方法是否正确获得我的输出或者我可以通过其他方式获得我的输出?
解决方案
推荐阅读
- jquery - $(this).val() 在加载时复制
- c++ - 我可以在 Visual Studio 2019 的项目下使用多个主要功能吗?
- node.js - 如何对嵌入式文档进行 $geoNear 聚合?
- javascript - 如何在不让用户单击按钮的情况下播放 javascript 声音?
- c# - 在 Visual Studio 依赖项错误中运行导入的 DLL 方法
- auth0 - Angular 8 中两个应用程序之间的无缝 SSO
- ibm-odm - 决策表中的操作表达式以将值添加到数组或 ArrayList
- javascript - 努力在问答游戏中定位按钮
- python - 如何将我的 matplotlib y 轴的比例更改为 y^2?
- delphi - 如何在 Delphi 的同一范围内重用相同的 TInterfacedObject 实例