Поиск по этому блогу

понедельник, 20 июня 2016 г.

Service Worker - прокси в браузере, управляемый скриптом с загружаемой страницы

Смотрим видео и умиляемся, до чего дошел прогресс. Вот, оказывается какие штуки появились, и в моих браузерах, и даже какие-то "воркеры" уже мне кто-то поставил... и в Chrome chrome://serviceworker-internals/, и в Mozilla about:debugging#workers
Понял, как они работают после того, как прочитал Register a Service Worker on the site из инструкции "Your first offline web app". А перед этим просмотрел несколько статей ...часть ссылок здесь.

New Service Worker debugging tools in Firefox Developer Edition 47 chrome://serviceworker-internals/
Register a Service Worker on the site
Push Simple Demo This demo shows how to register for push notifications and how to send them.
Service Worker API developer.mozilla.org
about:debugging#workers
html5bydemo.com Demo Source No Frame Sand Box

Register a service worker

The first step to making the app work offline is to register a service worker, a script that allows background functionality without the need for an open web page or user interaction.

This takes two simple steps:

Create a javascript file that will be the service worker.
Tell the browser to register the javascript file as the “service worker”.

First, create a blank file called sw.js

and place it in the /app folder. (This folder is the root folder for the app). You need to do this because the scope of a service worker (the set of urls that the ServiceWorker will load for) is defined by the directory where it resides. If it is not in the correct directory then the ServiceWorker will not be able to make the app work offline (this means you can’t place it in a script directory.)

Now open index.html in the /app folder and add the following code to the bottom.

In [ ]:
<script>
if('serviceWorker' in navigator) {
  navigator.serviceWorker
           .register('/sw.js')
           .then(function() { console.log("Service Worker Registered"); });
}
</script>

Start up a server on localhost and watch for any future changes to the site

In [ ]:
$ cd app
$ python -m SimpleHTTPServer 3000

This is important if you want to debug the install phase of a service worker

In [ ]:
#Open 
chrome://serviceworker-internals/ in Chrome.

From anothe articles:

Service worker concepts and usage (Service Worker API developer.mozilla.org)

A service worker is an event-driven worker registered against an origin and a path. It takes the form of a JavaScript file that can control the web page/site it is associated with, intercepting and modifying navigation and resource requests, and caching resources in a very granular fashion to give you complete control over how your app behaves in certain situations (the most obvious one being when the network is not available.)

A service worker is run in a worker context: it therefore has no DOM access, and runs on a different thread to the main JavaScript that powers your app, so it is not blocking. It is designed to be fully async; as a consequence, APIs such as synchronous XHR and localStorage can't be used inside a service worker.

Service workers only run over HTTPS, for security reasons. Having modified network requests wide open to man in the middle attacks would be really bad. In Firefox, Service Worker APIs are also hidden and cannot be used when the user is in private browsing mode.

about:debugging#workers - in my desktop Mozilla dev browser

In [ ]:
Service Workers

    https://serviceworke.rs/push-simple/service-worker.js
        Scopehttps://serviceworke.rs/push-simple/unregister

Shared Workers

Nothing yet.
Other Workers

    resource:///modules/sessionstore/SessionWorker.js

resource://gre/modules/osfile/osfile_async_worker.js
resource://gre/modules/PageThumbsWorker.js


Посты чуть ниже также могут вас заинтересовать

Комментариев нет: