diff --git a/background.js b/background.js index cf8421a..fe8a239 100644 --- a/background.js +++ b/background.js @@ -5,6 +5,9 @@ chrome.runtime.onInstalled.addListener(function () { chrome.storage.sync.set({ disableBubble: { key: 's', keyCode: 115 } }, function () { }); + chrome.storage.sync.set({ toggleBubble: { key: 'q', keyCode: 113 } }, function () { + }); + chrome.storage.sync.set({ persistent: { persistVisual: true } }, function () { }); diff --git a/lib/bubble.js b/lib/bubble.js index 94aa540..012f6b8 100644 --- a/lib/bubble.js +++ b/lib/bubble.js @@ -6,6 +6,8 @@ var clicked = false; var circ; var lastMousePos = null; + // as i am calling createcircle multiple times asynchronously, i thought keeping track of existences of the circle in the DOM will be a good idea. + var circExistsInDom = false; let pointPointDist = function (x1, y1, x2, y2) { var x = x1 - x2; @@ -43,6 +45,7 @@ }; let createCircle = function () { + if(circExistsInDom) return; circ = window.DomUtils.createElement('div'); var props = { position: 'absolute', borderRadius: '999px', @@ -56,8 +59,15 @@ } window.DomUtils.addElementList([circ], {}); + circExistsInDom = true; }; + let removeCircle = function () { + if(!circExistsInDom) return; + window.DomUtils.removeElement(circ); + circExistsInDom = false; + } + let refreshLinks = function () { links = updateRects(window.LocalHints.getLocalHints(false)); refreshBubble(lastMousePos); @@ -82,6 +92,8 @@ }; let loadSettings = function () { + // is it okkay to keep a listener always listening for toogle event? + addToggleListenerEvent(); chrome.storage.sync.get(['bubbleCursor'], function (data) { if (data.bubbleCursor.enabled === true) { addBubbleCursorEvents(); @@ -98,6 +110,27 @@ circ.style.display = visible ? 'block' : 'none'; }; + // standalone listener that never gets removed + const toggleListener = function (e) { + const code = (e.keyCode ? e.keyCode : e.which); + chrome.storage.sync.get(['toggleBubble', 'bubbleCursor', 'persistent'], function (obj) { + if (code == obj.toggleBubble.keyCode) { + const isEnabled = !obj.bubbleCursor.enabled; + chrome.storage.sync.set({ bubbleCursor: { enabled: isEnabled } }, function () { + if (isEnabled) { + createCircle(); + addBubbleCursorEvents(); + const isVisible = obj.persistent.persistVisual; + setBubbleVisibility(isVisible); + } else { + removeCircle(); + removeBubbleCursorEvents(); + } + }); + } + }); + } + let onKeyPress = function (e) { var code = (e.keyCode ? e.keyCode : e.which); chrome.storage.sync.get(['isVisible'], function (obj) { @@ -114,6 +147,7 @@ removeBubbleCursorEvents(); } }); + }; let onMouseClick = function (e) { @@ -163,6 +197,10 @@ } }; + const addToggleListenerEvent = function () { + document.addEventListener('keypress', toggleListener, false); + } + let addBubbleCursorEvents = function () { document.addEventListener('keypress', onKeyPress, false); document.addEventListener('click', onMouseClick, false); @@ -176,7 +214,7 @@ document.removeEventListener('click', onMouseClick, false); document.removeEventListener('mousemove', onMouseMove, false); window.removeEventListener('scroll', refreshLinks); - window.DomUtils.removeElement(circ); + removeCircle(); //might want to move this line somewhere else? shouldn't this function only remove the events, not dom elements? clearClosestElementHighlighting(prevClosest.element); }; diff --git a/options.html b/options.html index ebb8f4b..0b073ef 100644 --- a/options.html +++ b/options.html @@ -22,6 +22,10 @@
Disable bubble cursor without refreshing page with '' key.
+Toggle bubble cursor without refreshing page with '' key.
+ +