|
| 1 | +function init() { |
| 2 | + var slider = document.getElementById('playbackSpeed'); |
| 3 | + var output = document.getElementById('currentSpeed'); |
| 4 | + var resetButton = document.getElementById('resetPlaybackSpeed'); |
| 5 | + |
| 6 | + getFromStorage(null, function (savedData) { |
| 7 | + lastSetSpeed = savedData.lastSetSpeed || 1; |
| 8 | + |
| 9 | + setValueInner(lastSetSpeed); |
| 10 | + }); |
| 11 | + |
| 12 | + // attach the on-change and on-event handlers |
| 13 | + slider.oninput = setValue; |
| 14 | + resetButton.onclick = onResetClick; |
| 15 | + |
| 16 | + function setValue() { |
| 17 | + setValueInner(this.value); |
| 18 | + } |
| 19 | + |
| 20 | + function setValueInner(val) { |
| 21 | + output.innerHTML = val; |
| 22 | + slider.value = val; |
| 23 | + |
| 24 | + chrome.tabs.executeScript(null, { |
| 25 | + code: getUpdatePlaybackRateFn(val) |
| 26 | + }); |
| 27 | + |
| 28 | + setInStorage('lastSetSpeed', val); |
| 29 | + } |
| 30 | + |
| 31 | + function onResetClick() { |
| 32 | + setValueInner(1); |
| 33 | + } |
| 34 | + |
| 35 | + function getFromStorage(key, callback) { |
| 36 | + chrome.storage.sync.get(key, function(data) { |
| 37 | + callback(data); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + function setInStorage(key, data) { |
| 42 | + // Setting |
| 43 | + chrome.storage.sync.set(JSON.parse(`{"${key}": ${data}}`), function() { |
| 44 | + if (chrome.runtime.lastError) { |
| 45 | + console.error( |
| 46 | + 'Error setting ' + key + ' to ' + JSON.stringify(data) + ': ' + chrome.runtime.lastError.message |
| 47 | + ); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function getUpdatePlaybackRateFn(val) { |
| 54 | + return `document.querySelectorAll('video').forEach((videoElement) => { |
| 55 | + videoElement.playbackRate = ${val}; |
| 56 | + });`; |
| 57 | +} |
| 58 | + |
| 59 | +document.addEventListener('DOMContentLoaded', init); |
0 commit comments