From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../serviceworkerregistration/active/index.html | 127 +++++++++++ .../web/api/serviceworkerregistration/index.html | 245 +++++++++++++++++++++ .../pushmanager/index.html | 125 +++++++++++ .../unregister/index.html | 75 +++++++ .../serviceworkerregistration/update/index.html | 77 +++++++ 5 files changed, 649 insertions(+) create mode 100644 files/zh-cn/web/api/serviceworkerregistration/active/index.html create mode 100644 files/zh-cn/web/api/serviceworkerregistration/index.html create mode 100644 files/zh-cn/web/api/serviceworkerregistration/pushmanager/index.html create mode 100644 files/zh-cn/web/api/serviceworkerregistration/unregister/index.html create mode 100644 files/zh-cn/web/api/serviceworkerregistration/update/index.html (limited to 'files/zh-cn/web/api/serviceworkerregistration') diff --git a/files/zh-cn/web/api/serviceworkerregistration/active/index.html b/files/zh-cn/web/api/serviceworkerregistration/active/index.html new file mode 100644 index 0000000000..360536e262 --- /dev/null +++ b/files/zh-cn/web/api/serviceworkerregistration/active/index.html @@ -0,0 +1,127 @@ +--- +title: ServiceWorkerRegistration.active +slug: Web/API/ServiceWorkerRegistration/active +translation_of: Web/API/ServiceWorkerRegistration/active +--- +
{{SeeCompatTable}}{{APIRef("Service Workers API")}}
+ +

The active property of the {{domxref("ServiceWorkerRegistration")}} interface returns a service worker whose {{domxref("ServiceWorker.state")}} is activating or activated. This property is initially set to null.

+ +

An active worker controls a {{domxref("ServiceWorkerClient")}} if the client's URL falls within the scope of the registration (the scope option set when {{domxref("ServiceWorkerContainer.register")}} is first called.)

+ +
+

Note: This feature is available in Web Workers.

+
+ +

Syntax

+ +
sw = ServiceWorker.active
+
+ +

Value

+ +

A {{domxref("ServiceWorker")}} object, if it is currently in an activating or activated state.

+ +

Specifications

+ +
 
+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#service-worker-registration-active-attribute', 'ServiceWorkerRegistration.active')}}{{Spec2('Service Workers')}}Initial definition.
+  
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(40.0)}}{{ CompatGeckoDesktop("44.0") }}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
Available in web workers{{CompatVersionUnknown}}{{CompatGeckoDesktop("44.0")}}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatUnknown}}{{ CompatGeckoMobile("44.0") }}{{ CompatVersionUnknown }}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatUnknown}}
Available in web workers{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatUnknown}}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatUnknown}}
+
+ +

[1] Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR.)

+ +

See also

+ + diff --git a/files/zh-cn/web/api/serviceworkerregistration/index.html b/files/zh-cn/web/api/serviceworkerregistration/index.html new file mode 100644 index 0000000000..2b1a214546 --- /dev/null +++ b/files/zh-cn/web/api/serviceworkerregistration/index.html @@ -0,0 +1,245 @@ +--- +title: ServiceWorkerRegistration +slug: Web/API/ServiceWorkerRegistration +tags: + - API + - Draft + - Interface + - NeedsTranslation + - Offline + - Reference + - Service Workers + - ServiceWorkerRegistration + - TopicStub + - Workers +translation_of: Web/API/ServiceWorkerRegistration +--- +
+
{{SeeCompatTable}}{{APIRef("Service Workers API")}}
+
+ +

The ServiceWorkerRegistration interface of the ServiceWorker API represents the service worker registration. You register a service worker to control one or more pages that share the same origin.

+ +

The lifetime of a service worker registration is beyond that of the ServiceWorkerRegistration objects that represent them within the lifetime of their corresponding service worker clients. The browser maintains a persistent list of active ServiceWorkerRegistration objects.

+ +
+

Note: This feature is available in Web Workers.

+
+ +

Properties

+ +

Also implements properties from its parent interface, {{domxref("EventTarget")}}.

+ +
+
{{domxref("ServiceWorkerRegistration.scope")}} {{readonlyinline}}
+
Returns a unique identifier for a service worker registration. This must be on the same origin as the document that registers the {{domxref("ServiceWorker")}}.
+
{{domxref("ServiceWorkerRegistration.installing")}} {{readonlyinline}}
+
Returns a service worker whose state is installing. This is initially set to null.
+
{{domxref("ServiceWorkerRegistration.waiting")}} {{readonlyinline}}
+
Returns a service worker whose state is installed. This is initially set to null.
+
{{domxref("ServiceWorkerRegistration.active")}} {{readonlyinline}}
+
Returns a service worker whose state is either activating or activated. This is initially set to null. An active worker will control a {{domxref("ServiceWorkerClient")}} if the client's URL falls within the scope of the registration (the scope option set when {{domxref("ServiceWorkerContainer.register")}} is first called.)
+
{{domxref("serviceWorkerRegistration.periodicSync")}} {{non-standard_inline}} {{readonlyinline}}
+
Returns a reference to the {{domxref("PeriodicSyncManager")}} interface, which manages periodic background synchronization processes.
+
{{domxref("ServiceWorkerRegistration.pushManager")}} {{readonlyinline}}
+
Returns a reference to the {{domxref("PushManager")}} interface for managing push subscriptions including subscribing, getting an active subscription, and accessing push permission status.
+
{{domxref("ServiceWorkerRegistration.sync")}} {{non-standard_inline}} {{readonlyinline}} 
+
Returns a reference to the {{domxref("SyncManager")}} interface, which manages background synchronization processes.
+
+ +

Event handlers

+ +
+
{{domxref("ServiceWorkerRegistration.onupdatefound")}} {{readonlyinline}}
+
An EventListener property called whenever an event of type updatefound is fired; it is fired any time the {{domxref("ServiceWorkerRegistration.installing")}} property acquires a new service worker.
+
+ +

Methods

+ +

Also implements methods from its parent interface, {{domxref("EventTarget")}}.

+ +
+
{{domxref("ServiceWorkerRegistration.getNotifications()")}}
+
Returns a {{jsxref("Promise")}} that resolves to an array of {{domxref("Notification")}} objects.
+
{{domxref("ServiceWorkerRegistration.showNotification()")}}
+
Displays the notification with the requested title.
+
{{domxref("ServiceWorkerRegistration.update()")}}
+
Checks the server for an updated version of the service worker without consulting caches.
+
{{domxref("ServiceWorkerRegistration.unregister()")}}
+
Unregisters the service worker registration and returns a promise (see {{jsxref("Promise")}}). The service worker will finish any ongoing operations before it is unregistered.
+
+ +

Examples

+ +

This code snippet is from the service worker registration-events sample (live demo). The code checks to see if the browser supports service workers and if there's currently a service worker handling requests on this page for the current navigation.

+ +

Any new service workers are registered; if there's an existing service worker, the code overrides its default scope so that the registration applies to the current directory and everything underneath it. The example also reports any registration failures.

+ +
if ('serviceWorker' in navigator) {
+  document.querySelector('#availability').innerText = 'are';
+  document.querySelector('#controlled').innerText = navigator.serviceWorker.controller ? 'is' : 'is not';
+  navigator.serviceWorker.register('service-worker.js', {scope: './'}).then(function(registration) {
+    document.querySelector('#register').textContent = 'succeeded';
+  }).catch(function(error) {
+    document.querySelector('#register').textContent = 'failed: ' + error;
+  });
+} else {
+  document.querySelector('#availability').innerText = 'are not';
+}
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#service-worker-registration-obj', 'ServiceWorkerRegistration')}}{{Spec2('Service Workers')}}Initial definition.
{{SpecName('Push API', '#widl-ServiceWorkerRegistration-pushManager', 'PushManager')}}{{Spec2('Push API')}}Adds the {{domxref("PushManager","pushManager")}} property.
{{SpecName('Web Notifications')}}{{Spec2('Web Notifications')}}Adds the {{domxref("ServiceWorkerRegistration.showNotification()","showNotification()")}} method and the {{domxref("ServiceWorkerRegistration.getNotifications()","getNotifications()")}} method.
{{SpecName('Background Sync')}}{{Spec2('Background Sync')}}Adds the {{domxref("ServiceWorkerRegistration.sync","sync")}} property.
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(40.0)}}{{ CompatGeckoDesktop("44.0") }}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
Available in web workers{{CompatVersionUnknown}}{{CompatGeckoDesktop("44.0")}}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
getNotifications(), showNotification(){{CompatVersionUnknown}}{{CompatGeckoDesktop("46.0")}}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
sync property{{CompatChrome(49.0)}}    
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{ CompatGeckoMobile("44.0") }}{{ CompatVersionUnknown }}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatChrome(40.0)}}
Available in web workers{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("44.0")}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatVersionUnknown}}
getNotifications(), showNotification(){{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("46.0")}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatVersionUnknown}}
sync property{{CompatNo}}{{CompatNo}}     {{CompatChrome(49.0)}}
+
+ +

[1] Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR.)

+ +

See also

+ + diff --git a/files/zh-cn/web/api/serviceworkerregistration/pushmanager/index.html b/files/zh-cn/web/api/serviceworkerregistration/pushmanager/index.html new file mode 100644 index 0000000000..3708312f94 --- /dev/null +++ b/files/zh-cn/web/api/serviceworkerregistration/pushmanager/index.html @@ -0,0 +1,125 @@ +--- +title: ServiceWorkerRegistration.pushManager +slug: Web/API/ServiceWorkerRegistration/pushManager +translation_of: Web/API/ServiceWorkerRegistration/pushManager +--- +
{{SeeCompatTable}}{{APIRef("Service Workers API")}}
+ +

{{domxref("ServiceWorkerRegistration")}} 接口的 pushManager 属性返回用于管理推送订阅的 {{domxref("PushManager")}} 接口的引用。包括支持订阅,获取活动订阅和访问推送权限状态。

+ +

语法

+ +
myPushManager = ServiceWorker.pushManager
+
+ +

+ +

一个 {{domxref("PushManager")}} 对象。

+ +

示例

+ +
this.onpush = function(event) {
+  console.log(event.data);
+  // From here we can write the data to IndexedDB, send it to any open
+  // windows, display a notification, etc.
+}
+
+navigator.serviceWorker.register('serviceworker.js').then(
+  function(serviceWorkerRegistration) {
+    serviceWorkerRegistration.pushManager.subscribe().then(
+      function(pushSubscription) {
+        console.log(pushSubscription.subscriptionId);
+        console.log(pushSubscription.endpoint);
+        // The push subscription details needed by the application
+        // server are now available, and can be sent to it using,
+        // for example, an XMLHttpRequest.
+      }, function(error) {
+        // During development it often helps to log errors to the
+        // console. In a production environment it might make sense to
+        // also report information about errors back to the
+        // application server.
+        console.log(error);
+      }
+    );
+  });
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Push API', '#pushmanager-interface', 'PushManager')}}{{Spec2('Push API')}}Initial definition.
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(42.0)}}{{ CompatGeckoDesktop("44.0") }}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(42.0)}}
+
+ +

[1] Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR.)

+ +

参见

+ + diff --git a/files/zh-cn/web/api/serviceworkerregistration/unregister/index.html b/files/zh-cn/web/api/serviceworkerregistration/unregister/index.html new file mode 100644 index 0000000000..1e89dd3296 --- /dev/null +++ b/files/zh-cn/web/api/serviceworkerregistration/unregister/index.html @@ -0,0 +1,75 @@ +--- +title: ServiceWorkerRegistration.unregister() +slug: Web/API/ServiceWorkerRegistration/unregister +translation_of: Web/API/ServiceWorkerRegistration/unregister +--- +
{{SeeCompatTable}}{{APIRef("Service Workers API")}}
+ +

{{domxref("ServiceWorkerRegistration")}} 接口的 unregister 方法用于取消对service worker的注册并返回一个 {{jsxref("Promise")}}。没有找到注册时,这个 promise 返回 false ,否则,不论取消成功与否都返回 true (当其他人在同一作用域调用了 {{domxref("ServiceWorkerContainer.register")}} 可能取消失败)service worker 会在取消注册前完成一切正在进行的操作。

+ +
+

Note: 这一特性同样适用于 Web Workers.

+
+ +

语法

+ +
ServiceWorkerRegistration.unregister().then(function(boolean) {
+});
+ +

参数

+ +

None.

+ +

返回

+ +

Promise 返回一个bool值表示 service worker 是否被取消注册。

+ +

例子

+ +

下面的简单例子中注册了一个service worker,然后立即取消了:

+ +
if ('serviceWorker' in navigator) {
+  navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
+    // registration worked
+    console.log('Registration succeeded.');
+    registration.unregister().then(function(boolean) {
+      // if boolean = true, unregister is successful
+    });
+  }).catch(function(error) {
+    // registration failed
+    console.log('Registration failed with ' + error);
+  });
+};
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#service-worker-registration-unregister-method', 'ServiceWorkerRegistration.unregister()')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.ServiceWorkerRegistration.unregister")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/serviceworkerregistration/update/index.html b/files/zh-cn/web/api/serviceworkerregistration/update/index.html new file mode 100644 index 0000000000..aa89f12d69 --- /dev/null +++ b/files/zh-cn/web/api/serviceworkerregistration/update/index.html @@ -0,0 +1,77 @@ +--- +title: ServiceWorkerRegistration.update() +slug: Web/API/ServiceWorkerRegistration/update +tags: + - 方法 + - 更新 +translation_of: Web/API/ServiceWorkerRegistration/update +--- +
{{APIRef("Service Workers API")}}
+ +
{{domxref("ServiceWorkerRegistration")}} 的update方法尝试更新service worker。获得worker脚本的URL,逐字节匹配新获取的worker和当前的worker,存在差异的时候安装新的worker。获取worker脚本的更新操作会忽略浏览器缓存的24小时前的内容。
+ +
+

注意: 这个特性也应用于 Web Workers.

+
+ +

语法

+ +
ServiceWorkerRegistration.update();
+ +

参数

+ +

None.

+ +

返回

+ +

返回 {{domxref("Promise")}} 在resolve时对应一个 {{domxref("ServiceWorkerRegistration")}} 对象。

+ +

示例

+ +

下面的示例注册一个service worker,然后绑定事件到按钮,这样你可以有需要时,明确的更新server worker:

+ +
if ('serviceWorker' in navigator) {
+  navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
+    // registration worked
+    console.log('Registration succeeded.');
+    button.onclick = function() {
+      registration.update();
+    }
+  }).catch(function(error) {
+    // registration failed
+    console.log('Registration failed with ' + error);
+  });
+};
+ +

标准

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#service-worker-registration-update-method', 'ServiceWorkerRegistration.update()')}}{{Spec2('Service Workers')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("api.ServiceWorkerRegistration.update")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf