aboutsummaryrefslogtreecommitdiffstats
path: root/pages/popup.coffee
blob: 2ab97bef5c4193505964f0f0c509e6b6be2d4d45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
originalRule = undefined
originalPattern = undefined
originalPassKeys = undefined

reset = (initialize=false) ->
  document.getElementById("optionsLink").setAttribute "href", chrome.runtime.getURL("pages/options.html")
  chrome.tabs.getSelected null, (tab) ->
    isEnabled = chrome.extension.getBackgroundPage().isEnabledForUrl(url: tab.url)
    # Check if we have an existing exclusing rule for this page.
    if isEnabled.rule
      originalRule = isEnabled.rule
      originalPattern = originalRule.pattern
      originalPassKeys = originalRule.passKeys
    else
      # The common use case is to disable Vimium at the domain level.
      # This regexp will match "http://www.example.com/" from "http://www.example.com/path/to/page.html".
      domain = (tab.url.match(/[^\/]*\/\/[^\/]*\//) or tab.url) + "*"
      originalRule = null
      originalPattern = domain
      originalPassKeys = ""
    patternElement = document.getElementById("popupPattern")
    passKeysElement = document.getElementById("popupPassKeys")
    patternElement.value  = originalPattern
    passKeysElement.value = originalPassKeys
    if initialize
      # Activate <Ctrl-Enter> to save.
      for element in [ patternElement, passKeysElement ]
        element.addEventListener "keyup", (event) ->
          if event.ctrlKey and event.keyCode == 13
            addExclusionRule()
            window.close()
        element.addEventListener "focus", -> document.getElementById("helpText").style.display = "block"
        element.addEventListener "blur", -> document.getElementById("helpText").style.display = "none"
      # Focus passkeys with cursor at the end (but only when creating popup).
      passKeysElement.focus()
      passKeysElement.setSelectionRange(passKeysElement.value.length, passKeysElement.value.length)
    onChange()

onChange = ->
  # As the text in the popup's input elements is changed, update the the popup's buttons accordingly.
  # Aditionally, enable and disable those buttons as appropriate.
  pattern = document.getElementById("popupPattern").value.trim()
  passKeys = document.getElementById("popupPassKeys").value.trim()
  popupExclude = document.getElementById("popupExclude")

  document.getElementById("popupRemove").disabled =
    not (originalRule and pattern == originalPattern)

  if originalRule and pattern == originalPattern and passKeys == originalPassKeys
    popupExclude.disabled = true
    popupExclude.value = "Update Rule"

  else if originalRule and pattern == originalPattern
    popupExclude.disabled = false
    popupExclude.value = "Update Rule"

  else if originalRule
    popupExclude.disabled = false
    popupExclude.value = "Add Rule"

  else if pattern
    popupExclude.disabled = false
    popupExclude.value = "Add Rule"

  else
    popupExclude.disabled = true
    popupExclude.value = "Add Rule"

showMessage = do ->
  timer = null

  hideConfirmationMessage = ->
    document.getElementById("confirmationMessage").setAttribute "style", "display: none"
    timer = null

  (message) ->
    document.getElementById("confirmationMessage").setAttribute "style", "display: inline-block"
    document.getElementById("confirmationMessage").innerHTML = message
    clearTimeout(timer) if timer
    timer = setTimeout(hideConfirmationMessage,2000)

addExclusionRule = ->
  pattern = document.getElementById("popupPattern").value.trim()
  passKeys = document.getElementById("popupPassKeys").value.trim()
  chrome.extension.getBackgroundPage().addExclusionRule pattern, passKeys
  showMessage("Updated.")
  reset()

removeExclusionRule = ->
  pattern = document.getElementById("popupPattern").value.trim()
  chrome.extension.getBackgroundPage().removeExclusionRule pattern
  showMessage("Removed.")
  reset()

document.addEventListener "DOMContentLoaded", ->
  document.getElementById("popupExclude").addEventListener "click", addExclusionRule, false
  document.getElementById("popupRemove").addEventListener "click", removeExclusionRule, false
  for field in ["popupPattern", "popupPassKeys"]
    for event in ["input", "change"]
      document.getElementById(field).addEventListener event, onChange, false
  reset true