From 3a4db76703e852670ca1078b2205edffcaa3efcf Mon Sep 17 00:00:00 2001
From: gdh1995
Date: Sun, 4 Feb 2018 17:49:05 +0800
Subject: make the hooked addEventListener looks native
---
 content_scripts/injected.coffee | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/content_scripts/injected.coffee b/content_scripts/injected.coffee
index 4148ffa5..00b083b0 100644
--- a/content_scripts/injected.coffee
+++ b/content_scripts/injected.coffee
@@ -5,13 +5,32 @@
 
 injectedCode = () ->
   # Note the presence of "click" listeners installed with `addEventListener()` (for link hints).
-  _addEventListener = Element::addEventListener
+  _addEventListener = EventTarget::addEventListener
+  _toString = Function::toString
+  # Note some pages may override Element (see https://github.com/gdh1995/vimium-plus/issues/11)
+  EL = if typeof Element == "function" then Element else HTMLElement
+  Anchor = HTMLAnchorElement
 
-  Element::addEventListener = (type, listener, useCapture) ->
-    if type == "click"
-      try @setAttribute "_vimium-has-onclick-listener", ""
+  addEventListener = (type, listener, useCapture) ->
+    if type == "click" and this instanceof EL
+      try
+        unless this instanceof Anchor # just skip 
+          @setAttribute "_vimium-has-onclick-listener", ""
     _addEventListener?.apply this, arguments
 
+  newToString = () ->
+    real = if this == newToString then _toString else
+      if this == addEventListener then _addEventListener else
+      this
+    _toString.apply real, arguments
+
+  EventTarget::addEventListener = addEventListener
+  # Libraries like Angular/Zone and CKEditor check if element.addEventListener is native,
+  # so here we hook it to tell outsides it is exactly native.
+  # This idea is from https://github.com/angular/zone.js/pull/686,
+  # and see more discussions in https://github.com/ckeditor/ckeditor5-build-classic/issues/34
+  Function::toString = newToString
+
 script = document.createElement "script"
 script.textContent = "(#{injectedCode.toString()})()"
 (document.head || document.documentElement).appendChild script
-- 
cgit v1.2.3
From a5e0f989c3f02c2bbe16517ae67a3e151860c1cc Mon Sep 17 00:00:00 2001
From: Stephen Blott
Date: Sun, 4 Feb 2018 12:37:54 +0000
Subject: Tweak #2941.
---
 content_scripts/injected.coffee | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/content_scripts/injected.coffee b/content_scripts/injected.coffee
index 00b083b0..5b614763 100644
--- a/content_scripts/injected.coffee
+++ b/content_scripts/injected.coffee
@@ -13,9 +13,8 @@ injectedCode = () ->
 
   addEventListener = (type, listener, useCapture) ->
     if type == "click" and this instanceof EL
-      try
-        unless this instanceof Anchor # just skip 
-          @setAttribute "_vimium-has-onclick-listener", ""
+      unless this instanceof Anchor # Just skip .
+        try @setAttribute "_vimium-has-onclick-listener", ""
     _addEventListener?.apply this, arguments
 
   newToString = () ->
@@ -28,7 +27,7 @@ injectedCode = () ->
   # Libraries like Angular/Zone and CKEditor check if element.addEventListener is native,
   # so here we hook it to tell outsides it is exactly native.
   # This idea is from https://github.com/angular/zone.js/pull/686,
-  # and see more discussions in https://github.com/ckeditor/ckeditor5-build-classic/issues/34
+  # and see more discussions in https://github.com/ckeditor/ckeditor5-build-classic/issues/34.
   Function::toString = newToString
 
 script = document.createElement "script"
-- 
cgit v1.2.3