0

I need to define a method inside Element.prototype in my userscript but when I try to to that, I get weird errors:

// ==UserScript==
// <...>
// @grant        none
// ==/UserScript==

;[Element.prototype, Text.prototype].forEach(e => {
  e.findParent = function(selector) {
    let node = this
    while(node && !node.matches(selector)) {
      node = node.parentNode
      if (! node.matches) return null;
    }
    return node
  }
}

[...].forEach(...) is not a function

function augmentPrototype(e) {
  e.findParent = function(selector) {
    /* <...> */
  }
}
augmentPrototype(Element.prototype)

augmentPrototype is not a function

(e => {
  e.findParent = function(selector) {
    /* <...> */
  }

})(Element.prototype)

(intermediate value)(...) is not a function

It seems whatever function is being called on Element.prototype, it immediately becomes "not a function" to Greasemonkey, even [].forEach.

No such problem in normal Firefox console. Also I modify Event.prototype as well and it works fine.

Update: same problem on Tampermonkey for Firefox, so it's not just Greasemonkey's problem.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Juribiyan
  • 700
  • 8
  • 25

1 Answers1

-1

Sorry, false alarm. Turns out, next to this in my code I have an immediately invoking function starting with (... and I don't use semicolons. Leading semicolon just before that IIF fixed the problem. Apparently automatic semicolon insertion works slightly differently in Firefox because it worked fine in Chrome.

Juribiyan
  • 700
  • 8
  • 25
  • 1
    This means that the real problem was something you didn't show us, which explains the nonfunctional code and mismatched results in the question. You should probably just delete this Q&A. – Brock Adams Jul 10 '17 at 08:35