From 1ba221415d84262296d286d4e8b2db0ee2b563f0 Mon Sep 17 00:00:00 2001 From: Philip Strecker Date: Sat, 22 Aug 2020 17:37:55 +0200 Subject: [PATCH] fixed multiple bugs --- contentscript.js | 31 ++++++++++++++++++++++++------- manifest.json | 2 +- popup.js | 9 ++++++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/contentscript.js b/contentscript.js index 76f3453..1ce799c 100644 --- a/contentscript.js +++ b/contentscript.js @@ -4,12 +4,21 @@ const STATUS_KEY = "video-volume-status-per-website"; let store = chrome.storage || window.storage; + let runtime = chrome.runtime || window.runtime; + + runtime.onMessage.addListener(function(volume) { + let host = window.location.host; + data = {}; + data[STORAGE_KEY + '_' + host] = Math.round(volume); + saveVolumne(data); + }); lock = false; function setVolume() { let host = window.location.host; let key = STATUS_KEY + '_' + host; let websiteKey = STORAGE_KEY + '_' + host; + store.local.get([key, MAX_VOL_STORAGE_KEY, websiteKey, STORAGE_KEY], function(data) { var videoTags = Array.from(document.getElementsByTagName("video")); @@ -59,12 +68,13 @@ return; } videoTags[i].onvolumechange = function (argument) { + if (argument.target.muted || argument.target.paused) { + return; + } lock = true; var key = {}; key[STORAGE_KEY + '_' + host] = Math.round(argument.target.volume * 100) - store.local.set(key, function() { - lock = false; - }); + saveVolumne(key); } } for (var i in audioTags) { @@ -75,18 +85,25 @@ return; } audioTags[i].onvolumechange = function (argument) { + if (argument.target.muted || argument.target.paused) { + return; + } lock = true; var key = {}; - key[STORAGE_KEY + '_' + host] = Math.round(argument.target.volume * 100) - store.local.set(key, function() { - lock = false; - }); + key[STORAGE_KEY + '_' + host] = Math.round(argument.target.volume * 100); + saveVolumne(key); } } }); } + function saveVolumne(data) { + store.local.set(data, function() { + lock = false; + }); + } + function parseHostFromURL(url) { var parser = document.createElement('a'); parser.href = url; diff --git a/manifest.json b/manifest.json index 0b57435..03e4532 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name" : "Persistent Video/Audio Volume", - "version" : "0.10.0", + "version" : "0.10.1", "manifest_version" : 2, "description" : "Saves video and audio volume", "permissions": ["storage", "tabs", ""], diff --git a/popup.js b/popup.js index 08b992b..eccee4a 100644 --- a/popup.js +++ b/popup.js @@ -5,6 +5,8 @@ const STATUS_KEY = "video-volume-status-per-website"; let store = chrome.storage || window.storage; let tbs = chrome.tabs || window.tabs; +let runtime = chrome.runtime || window.runtime; + document.addEventListener('DOMContentLoaded', function() { @@ -29,7 +31,11 @@ document.addEventListener('DOMContentLoaded', function() { maxGlobalText.textContent = volume + '%'; } + var currentTab; + + tbs.query({active: true, currentWindow: true}, function(tabs) { + currentTab = tabs[0]; host = parseHostFromURL(tabs[0].url); let key = STATUS_KEY + '_' + host; store.local.get(key, function(data) { @@ -45,7 +51,7 @@ document.addEventListener('DOMContentLoaded', function() { store.local.get(STORAGE_KEY + '_' + host, function(data) { var volume = data[STORAGE_KEY + '_' + host]; - if (!volume) { + if (isNaN(volume)) { volume = 50; } websiteSlider.value = volume; @@ -76,6 +82,7 @@ document.addEventListener('DOMContentLoaded', function() { store.local.set(key, function() { setWebsiteLabel(websiteSlider.value); }); + tbs.sendMessage(currentTab.id, websiteSlider.value); }); status.addEventListener('change', () => {