Answer the question
In order to leave comments, you need to log in
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");
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');
}
}
if ( 'undefined' === typeof window) {
console.log('work');
importScripts('https://push.esputnik.com/service-worker.js');
}
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);
});
};
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question