80868

How to load Javascript file in a Service Worker dynamically?

<h3>Question</h3>

I can't use any solutions that involve creating a <script> tag dynamically because document does not exist in a ServiceWorker context.

In this case I want to load analytics toolkit so I can track when SW-triggered Notifications are interacted with by the user.

Any ideas?


<h3>Answer1:</h3>

That sounds like the intended use case of the importScripts() method.

The imported script shares the global scope with your service worker, so modifying self via, e.g., self.addEventListener() or self.someIdentifier = 'blah' will have the same effect as it would from within the main service worker script.

来源:https://stackoverflow.com/questions/36251929/how-to-load-javascript-file-in-a-service-worker-dynamically

Recommend

  • Hive explain plan understanding
  • Why do I need to initialize an int variable to 0?
  • How can I start a .NET application from Java on Linux, Mac and Windows?
  • Invoke method on generic type?
  • css hide “Choose File” button but display file after select
  • Ionic 3: The pipe '' could not be found [duplicate]
  • How to Upload Large Video FIle on server?
  • How do i create a connection between my iPhone app with my mac app? [duplicate]
  • Creating a Blog Comments and Reply section using ASP.NET MVC 4 (nested collections)
  • Cosmos DB out of Memory exception while executing stored procedure
  • How to download an image using a URL with a query string instead of image path?
  • Parameterize the approver detail in Promoted Build Plugin in Jenkins
  • No OQGraph in my MariaDB?
  • Unknown function 'algo.getNodeById'
  • Using database with Store Locator on Google Maps API
  • How to get service executable file path
  • How to get checkbox state in a gridview
  • Having several issues with a Python service for Windows
  • Efficiently comparing generated code between two Git revisions of the source
  • typescript multidimensional array with different types
  • React Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Velocity (VM) template request parameters: Getting GET variables
  • android 3G signal strength
  • How to read contents of a directory recursively in Linux Kernel?
  • UIImage to UIColor array of pixel colors
  • Enable CORS on Tomcat 8.0.30
  • Run a form (insert/update/delete) from within a div using jquery
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • Change the color of the legend text in forceNetwork for networkD3
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • Bind selectedDates Aggregation for Calendar
  • Computing the discrete fourier transform of audio data with FFTW
  • Call Microservice from another Microservice within Docker
  • Using Service Component Runtime
  • convert json to excel in java
  • Create/delete users from text file using Bash script
  • How to check if object is null in Java?