Add option for customizing the clipboard monitor update interval

This commit is contained in:
Kamil Tomala 2017-08-11 20:55:54 +02:00
parent 387397878b
commit 9f34c1ce74
4 changed files with 29 additions and 7 deletions

View File

@ -16,6 +16,7 @@ browser.storage.onChanged.addListener((changes, area) => {
options[key] = changes[key].newValue options[key] = changes[key].newValue
} }
} }
updateTimer()
} }
}) })
@ -63,14 +64,22 @@ function checkClipboard() {
} }
function updateTimer() { function updateTimer() {
function stop() {
clearInterval(timer.id)
timer = null
}
function start() {
const id = setInterval(checkClipboard, options.monitorInterval)
timer = { id, interval: options.monitorInterval }
}
if(listeningTabs.length > 0) { if(listeningTabs.length > 0) {
if(timer === null) { if(timer === null) {
const id = setInterval(checkClipboard, 1000) start()
timer = { id } } else if(timer.interval !== options.monitorInterval) {
stop()
start()
} }
} else { } else {
if(timer !== null) { stop()
clearInterval(timer.id)
}
} }
} }

View File

@ -1,4 +1,5 @@
const defaultOptions = { const defaultOptions = {
elemName: 'p', elemName: 'p',
containerSelector: 'body' containerSelector: 'body',
monitorInterval: 300
} }

View File

@ -16,6 +16,10 @@
<label for="container-selector">Container selector </label> <label for="container-selector">Container selector </label>
<input id="container-selector" type="text"/> <input id="container-selector" type="text"/>
</div> </div>
<div class="pure-control-group">
<label for="monitor-interval">Timer interval </label>
<input id="monitor-interval" type="number" min="100" step="10"/>
</div>
</fieldset> </fieldset>
</form> </form>
</body> </body>

View File

@ -1,13 +1,21 @@
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
const elemName = document.querySelector("#elem-name"), const elemName = document.querySelector("#elem-name"),
containerSelector = document.querySelector("#container-selector") containerSelector = document.querySelector("#container-selector"),
monitorInterval = document.querySelector("#monitor-interval")
const storage = browser.storage.local const storage = browser.storage.local
storage.get(defaultOptions).then(o => { storage.get(defaultOptions).then(o => {
elemName.value = o.elemName elemName.value = o.elemName
containerSelector.value = o.containerSelector containerSelector.value = o.containerSelector
monitorInterval.value = o.monitorInterval
}) })
elemName.onchange = () => storage.set({ elemName: elemName.value }) elemName.onchange = () => storage.set({ elemName: elemName.value })
containerSelector.onchange = () => storage.set({ containerSelector: containerSelector.value }) containerSelector.onchange = () => storage.set({ containerSelector: containerSelector.value })
monitorInterval.onchange = () => {
const newVal = monitorInterval.value
if(newVal >= 100) {
storage.set({ monitorInterval: monitorInterval.value })
}
}
}) })