var audioElement; var interer; var start = false; chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { console.log(request.greeting); if(request.greeting == "start"){ if(start == false){ start = true; var link = document.createElement("link"); link.href = chrome.extension.getURL("animationwashingmachine.css"); link.type = "text/css"; link.rel = "stylesheet"; document.getElementsByTagName("head")[0].appendChild(link); audioElement = document.createElement('audio'); audioElement.setAttribute('src', chrome.extension.getURL("start.mp3")); audioElement.setAttribute('autoplay', 'autoplay'); audioElement.setAttribute('loop', 'true'); audioElement.play(); $('body').css({"-webkit-animation-duration": "8.229s"}); $('body').css({"-webkit-animation-name": "washingmachinestart"}); $('body').css({"-webkit-animation-timing-function": "ease-in-out" }); $('body').css({"-webkit-animation-play-state": "running"}); $('body').css({"-webkit-animation-iteration-count": "infinite" }); } } if(request.greeting == "stop"){ start = false; audioElement.play(); clearTimeout(interer); $('body').css("-webkit-animation", "none"); audioElement.src = ""; } } );