H
H
Hailgrim2018-02-07 14:55:24
JavaScript
Hailgrim, 2018-02-07 14:55:24

How to fix importScripts is not defined?

Hello. There was a need to connect a service for sending push notifications to the site. The js file provided by the service for the functionality had only one line: "importScripts(' https://link-to-their-service/file.js ')". After connecting, the error "importScripts is not defined" began to appear in the browser console. I googled, tried to connect the way they wrote in the tips. But it didn't help. Sometimes the error disappeared, but the functionality itself did not work (a window should appear asking you to allow notifications).
What I have tried:
1.

var worker = new Worker('https://push.esputnik.com/service-worker.js');
worker.postMessage("test");

No reaction.
2.
if( 'function' === typeof importScripts) {
   importScripts('https://push.esputnik.com/service-worker.js');
   addEventListener('message', onMessage);
   function onMessage(e) { 
     // do some work here 
     console.log('work');
   }    
}

The condition didn't work.
3.
if ( 'undefined' === typeof window) {
  console.log('work');
  importScripts('https://push.esputnik.com/service-worker.js');
}

The condition didn't work.
4. Just for the sake of experiment...
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('https://push.esputnik.com/service-worker.js').then(function(reg) {
    // регистрация сработала
    console.log('Registration succeeded. Scope is ' + reg.scope);
  }).catch(function(error) {
    // регистрация прошла неудачно
    console.log('Registration failed with ' + error);
  });
};

Registration failed... Gave a security error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Polyakov, 2018-02-07
@challenger1401

Uncaught DOMException: Failed to construct 'Worker': Script at ' https://push.esputnik.com/service-worker.js ' cannot be accessed from origin ' https://toster.ru '.
at :1:14 is what the chrome console says.
Have you tried saving the file locally?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question