diff options
| author | Stephen Blott | 2017-12-14 14:08:35 +0000 |
|---|---|---|
| committer | Stephen Blott | 2017-12-14 14:12:08 +0000 |
| commit | f2d7e185c704e3cf8f6ca0c9d3d4da2498610475 (patch) | |
| tree | 965411d361a2c3405d94015a3b8dc9aab6225ca8 | |
| parent | 797e25231b306403167f5a3f90d6cea6d9ba6dda (diff) | |
| download | vimium-f2d7e185c704e3cf8f6ca0c9d3d4da2498610475.tar.bz2 | |
Detect click listeners for link hints.
This is a simpler version of #1167. It detects clickable elements with
listeners added with `addEventListener()`.
It includes some of @mrmr1993's ideas from #1167 (in fact, it's mostly
those ideas tweaked into a slightly different form).
| -rw-r--r-- | content_scripts/injected.coffee | 20 | ||||
| -rw-r--r-- | content_scripts/link_hints.coffee | 3 | ||||
| -rw-r--r-- | manifest.json | 6 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.coffee | 1 |
4 files changed, 30 insertions, 0 deletions
diff --git a/content_scripts/injected.coffee b/content_scripts/injected.coffee new file mode 100644 index 00000000..d833d09c --- /dev/null +++ b/content_scripts/injected.coffee @@ -0,0 +1,20 @@ +# The code in `injectedCode()`, below, is injected into the page's own execution context. +# +# This is based on method 2b here: http://stackoverflow.com/a/9517879, and +# @mrmr1993's #1167. + +window.vimiumOnClickAttributeName = "_vimium-has-onclick-listener" + +injectedCode = (vimiumOnClickAttributeName) -> + # Note the presence of "click" listeners installed with `addEventListener()` (for link hints). + _addEventListener = Element::addEventListener + + Element::addEventListener = (type, listener, useCapture) -> + @setAttribute vimiumOnClickAttributeName, "" if type == "click" + _addEventListener.apply this, arguments + +script = document.createElement "script" +script.textContent = "(#{injectedCode.toString()})('#{vimiumOnClickAttributeName}')" +(document.head || document.documentElement).appendChild script +script.remove() + diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 0592c96d..e5c6b3ea 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -706,6 +706,9 @@ LocalHints = isClickable = true reason = "Open." + # Detect elements with "click" listeners installed with `addEventListener()`. + isClickable ||= element.hasAttribute vimiumOnClickAttributeName + # An element with a class name containing the text "button" might be clickable. However, real clickables # are often wrapped in elements with such class names. So, when we find clickables based only on their # class name, we mark them as unreliable. diff --git a/manifest.json b/manifest.json index 86223848..ce83256a 100644 --- a/manifest.json +++ b/manifest.json @@ -70,6 +70,12 @@ "match_about_blank": true }, { + "matches": ["<all_urls>"], + "js": ["content_scripts/injected.js"], + "run_at": "document_start", + "all_frames": true + }, + { "matches": ["file:///", "file:///*/"], "css": ["content_scripts/file_urls.css"], "run_at": "document_start", diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index 6e422d46..d9de743e 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -51,6 +51,7 @@ activateLinkHintsMode = -> # link hinting modes. # createGeneralHintTests = (isFilteredMode) -> + window.vimiumOnClickAttributeName = "does-not-matter" context "Link hints", |
