2

How can we to extend element methods in JSDOM to all opened pages?

// load the module and few pages
const JSDOM = require("jsdom").JSDOM;
var dom1 = new JSDOM("<p>1st</p><p>2nd</p>");
var dom2 = new JSDOM("<p>1st</p><p>2nd</p>");
...

// add methods such as: map, filter, searchText...
var window = dom1.window;
window.NodeList.prototype.map = 
    function (cb) { return Array.from(this).map(cb) };
...

// Execution:
dom1.window.document.querySelectorAll('p').map(p=> p.textContent);
// returns: [ '1st', '2nd' ]
dom2.window.document.querySelectorAll('p').map(p=> p.textContent);
// throws an error

Is it possible to extend NodeList ones, without loading it for every new document?
Something more like:

JSDOM.installedInterfaces.NodeList.prototype.map = ...
Dani-Br
  • 2,289
  • 5
  • 25
  • 32
  • did you ever work this out? I'd like to add a querySelectorAll polyfill described in https://stackoverflow.com/a/17989803/1243605 but need access to JSDOM's Element prototype. – ericP Jun 27 '21 at 21:39

0 Answers0