diff --git a/content/content.js b/content/content.js index d70d463..5335a00 100644 --- a/content/content.js +++ b/content/content.js @@ -1,4 +1,3 @@ -var videoDetected = false; var videoChecker; function checkForVideos() { @@ -6,9 +5,9 @@ function checkForVideos() { if (videos.length > 0) { chrome.storage.local.get("state", function(value) { const enabled = (value["state"] !== "disabled"); - if (!videoDetected) { - // A video was detected, previously none were. - videoDetected = true; + // Check whether video previously detected (i.e. already listening to + // storage changes). + if (!chrome.storage.onChanged.hasListener(handleStorageChanges)) { // Listen to changes to the storage made by the popup. chrome.storage.onChanged.addListener(handleStorageChanges); if (enabled) { @@ -20,9 +19,7 @@ function checkForVideos() { } updateUnprocessedVideos(videos, enabled); }); - } else if (videoDetected) { - // A video was previously detected, but there aren't any now. - videoDetected = false; + } else if (chrome.storage.onChanged.hasListener(handleStorageChanges)) { // No more video: do not react to subsequent storage changes. chrome.storage.onChanged.removeListener(handleStorageChanges); // Notify background so that tab icon is changed to pause. diff --git a/tests/spec/ContentSpec.js b/tests/spec/ContentSpec.js index b16ad66..4e610cb 100644 --- a/tests/spec/ContentSpec.js +++ b/tests/spec/ContentSpec.js @@ -32,68 +32,63 @@ describe("Content script", function() { }); it("should do nothing if no videos are detected and none previously were", function() { - videoDetected = false; - checkForVideos(); expect(chrome.storage.local.get.notCalled).toBeTruthy(); - expect(videoDetected).toBeFalsy(); + expect(chrome.storage.onChanged.addListener.notCalled).toBeTruthy(); expect(chrome.storage.onChanged.removeListener.notCalled).toBeTruthy(); expect(chrome.runtime.sendMessage.notCalled).toBeTruthy(); }); it("should remove listener and notify background if all previsouly detected videos are gone", function() { - videoDetected = true; + chrome.storage.onChanged.addListener(handleStorageChanges); checkForVideos(); expect(chrome.storage.local.get.notCalled).toBeTruthy(); expect(chrome.storage.onChanged.removeListener.calledOnce).toBeTruthy(); - expect(videoDetected).toBeFalsy(); expect(chrome.runtime.sendMessage.withArgs({ active : false }).calledOnce).toBeTruthy(); }); - it("should update unprocessed videos if some videos were previsouly detected", function() { + it("should add listener and update unprocessed videos if videos detected for the first time, but not notify background if extension disabled", function() { document.body.appendChild(video); - videoDetected = true; chrome.storage.local.get.withArgs("state").yields({ - state : "enabled" + state : "disabled" }); spyOn(window, "updateUnprocessedVideos"); checkForVideos(); expect(chrome.storage.local.get.calledOnce).toBeTruthy(); - expect(videoDetected).toBeTruthy(); - expect(chrome.storage.onChanged.addListener.notCalled).toBeTruthy(); + expect(chrome.storage.onChanged.addListener.calledOnce).toBeTruthy(); expect(chrome.runtime.sendMessage.notCalled).toBeTruthy(); expect(window.updateUnprocessedVideos.calls.mostRecent().args[0][0]).toEqual(video); - expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeTruthy(); + expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeFalsy(); }); - it("should add listener and update unprocessed videos if videos detected for the first time, but not notify background if extension disabled", function() { + it("should add listener, update unprocessed videos and notify background if videos detected for the first time and extension enabled", function() { document.body.appendChild(video); - videoDetected = false; chrome.storage.local.get.withArgs("state").yields({ - state : "disabled" + state : "enabled" }); spyOn(window, "updateUnprocessedVideos"); checkForVideos(); expect(chrome.storage.local.get.calledOnce).toBeTruthy(); - expect(videoDetected).toBeTruthy(); expect(chrome.storage.onChanged.addListener.calledOnce).toBeTruthy(); - expect(chrome.runtime.sendMessage.notCalled).toBeTruthy(); + expect(chrome.runtime.sendMessage.withArgs({ + active : true + }).calledOnce).toBeTruthy(); expect(window.updateUnprocessedVideos.calls.mostRecent().args[0][0]).toEqual(video); - expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeFalsy(); + expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeTruthy(); }); - - it("should add listener, update unprocessed videos and notify background if videos detected for the first time and extension enabled", function() { + + it("should update unprocessed videos but not notify background if some videos were previsouly detected", function() { document.body.appendChild(video); - videoDetected = false; + chrome.storage.onChanged.addListener(handleStorageChanges); chrome.storage.local.get.withArgs("state").yields({ state : "enabled" }); @@ -102,14 +97,11 @@ describe("Content script", function() { checkForVideos(); expect(chrome.storage.local.get.calledOnce).toBeTruthy(); - expect(videoDetected).toBeTruthy(); - expect(chrome.storage.onChanged.addListener.calledOnce).toBeTruthy(); - expect(chrome.runtime.sendMessage.withArgs({ - active : true - }).calledOnce).toBeTruthy(); + expect(chrome.runtime.sendMessage.notCalled).toBeTruthy(); expect(window.updateUnprocessedVideos.calls.mostRecent().args[0][0]).toEqual(video); expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeTruthy(); }); + }); describe("Update unprocessed videos", function() {