我正在使用 html 文件输入打开相机并为我的 PWA 拍照。
<input type="file" accept="image/*" capture="camera" name="photo" id="photo-input-js" data-project-id="<?php echo $projectId ?>">
// this element triggers the input
<li class="menu-item <?php echo $current_page == 'camera' ? 'is-active' : '' ?>" id="camera-tab">
<a href="<?php echo site_url("photos/openCamera/". $projectId) ?>" id="open-camera-js">
<div class="icon icon-camera"></div>
<span class="d-none d-md-block ">Camera</span>
</a>
</li>
Run Code Online (Sandbox Code Playgroud)
Javascript:
// open camera
$(document).on('click', '#open-camera-js', function(e) {
e.preventDefault();
$(".menu-item").removeClass('is-active');
$("#camera-tab").addClass('is-active');
// check support for geolocation/ask for permissions
if (!navigator.geolocation) {
throw new Error('Unsupproted device');
}
// open the file …Run Code Online (Sandbox Code Playgroud) 有没有办法检测用户是否已使用服务人员连接回网络/在线?
我想在用户返回网络时获取一些数据。
它可能可以在 fetch 事件处理程序中完成,但我更喜欢更少的 hacky 解决方案。
我想出的黑客解决方案的伪代码:
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request)
.then(function(res) {
// check previous status
// if previous status == offline, user is back online
// do back online stuff...
// set status to online if previous status is offline
})
.catch(function(err) {
// set status to offline
})
);
});
Run Code Online (Sandbox Code Playgroud) javascript service-worker progressive-web-apps service-worker-events