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
}
}
updateTimer()
}
})
@ -63,14 +64,22 @@ function checkClipboard() {
}
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(timer === null) {
const id = setInterval(checkClipboard, 1000)
timer = { id }
start()
} else if(timer.interval !== options.monitorInterval) {
stop()
start()
}
} else {
if(timer !== null) {
clearInterval(timer.id)
}
stop()
}
}

View File

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

View File

@ -16,6 +16,10 @@
<label for="container-selector">Container selector </label>
<input id="container-selector" type="text"/>
</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>
</form>
</body>

View File

@ -1,13 +1,21 @@
document.addEventListener("DOMContentLoaded", () => {
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
storage.get(defaultOptions).then(o => {
elemName.value = o.elemName
containerSelector.value = o.containerSelector
monitorInterval.value = o.monitorInterval
})
elemName.onchange = () => storage.set({ elemName: elemName.value })
containerSelector.onchange = () => storage.set({ containerSelector: containerSelector.value })
monitorInterval.onchange = () => {
const newVal = monitorInterval.value
if(newVal >= 100) {
storage.set({ monitorInterval: monitorInterval.value })
}
}
})