aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/dom_utils.coffee12
-rw-r--r--lib/find_mode_history.coffee50
2 files changed, 62 insertions, 0 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index 7c47179c..9658df2b 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -326,6 +326,18 @@ DomUtils =
document.body.removeChild div
coordinates
+ getSelectionFocusElement: ->
+ sel = window.getSelection()
+ if not sel.focusNode?
+ null
+ else if sel.focusNode == sel.anchorNode and sel.focusOffset == sel.anchorOffset
+ # The selection either *is* an element, or is inside an opaque element (eg. <input>).
+ sel.focusNode.childNodes[sel.focusOffset]
+ else if sel.focusNode.nodeType != sel.focusNode.ELEMENT_NODE
+ sel.focusNode.parentElement
+ else
+ sel.focusNode
+
# Get the text content of an element (and its descendents), but omit the text content of previously-visited
# nodes. See #1514.
# NOTE(smblott). This is currently O(N^2) (when called on N elements). An alternative would be to mark
diff --git a/lib/find_mode_history.coffee b/lib/find_mode_history.coffee
new file mode 100644
index 00000000..ff660bd2
--- /dev/null
+++ b/lib/find_mode_history.coffee
@@ -0,0 +1,50 @@
+# NOTE(mrmr1993): This is under lib/ since it is used by both content scripts and iframes from pages/.
+# This implements find-mode query history (using the "findModeRawQueryList" setting) as a list of raw queries,
+# most recent first.
+FindModeHistory =
+ storage: chrome?.storage.local # Guard against chrome being undefined (in the HUD iframe).
+ key: "findModeRawQueryList"
+ max: 50
+ rawQueryList: null
+
+ init: ->
+ @isIncognitoMode = chrome?.extension.inIncognitoContext
+
+ return unless @isIncognitoMode? # chrome is undefined in the HUD iframe during tests, so we do nothing.
+
+ unless @rawQueryList
+ @rawQueryList = [] # Prevent repeated initialization.
+ @key = "findModeRawQueryListIncognito" if @isIncognitoMode
+ @storage.get @key, (items) =>
+ unless chrome.runtime.lastError
+ @rawQueryList = items[@key] if items[@key]
+ if @isIncognitoMode and not items[@key]
+ # This is the first incognito tab, so we need to initialize the incognito-mode query history.
+ @storage.get "findModeRawQueryList", (items) =>
+ unless chrome.runtime.lastError
+ @rawQueryList = items.findModeRawQueryList
+ @storage.set findModeRawQueryListIncognito: @rawQueryList
+
+ chrome.storage.onChanged.addListener (changes, area) =>
+ @rawQueryList = changes[@key].newValue if changes[@key]
+
+ getQuery: (index = 0) ->
+ @rawQueryList[index] or ""
+
+ saveQuery: (query) ->
+ if 0 < query.length
+ @rawQueryList = @refreshRawQueryList query, @rawQueryList
+ newSetting = {}; newSetting[@key] = @rawQueryList
+ @storage.set newSetting
+ # If there are any active incognito-mode tabs, then propagte this query to those tabs too.
+ unless @isIncognitoMode
+ @storage.get "findModeRawQueryListIncognito", (items) =>
+ if not chrome.runtime.lastError and items.findModeRawQueryListIncognito
+ @storage.set
+ findModeRawQueryListIncognito: @refreshRawQueryList query, items.findModeRawQueryListIncognito
+
+ refreshRawQueryList: (query, rawQueryList) ->
+ ([ query ].concat rawQueryList.filter (q) => q != query)[0..@max]
+
+root = exports ? window
+root.FindModeHistory = FindModeHistory