greasemonkey / greasemonkey

Greasemonkey is a user script manager for Firefox.
http://www.greasespot.net/
Other
2.31k stars 327 forks source link

Using Object.entries on document.querySelector results in an Array with length 0 #3134

Open sticks-stuff opened 2 years ago

sticks-stuff commented 2 years ago
// ==UserScript==
// @name         Test
// @run-at       document-idle
// @include      https://twitter.com/*
// @include      https://mobile.twitter.com/*
// ==/UserScript==

console.log("startup");

setInterval(() => {
  console.log(Object.entries(document.querySelector("main[role='main'] section[role='region']").parentElement.parentElement));
}, 1000)

Running Object.entries(document.querySelector("main[role='main'] section[role='region']").parentElement.parentElement) on any tweet returns an array of seven items, but returns nothing when running through a userscript like above.

It should be noted that this userscript works perfectly on TamperMonkey on Chrome

erosman commented 1 year ago

Object.entries(), Object.keys(), Object.values() of DOM elements are only available in page context and not in contentScripts context.

However, in Firefox you can get them using wrappedJSObject.

See also: https://github.com/erosman/support/issues/429#issuecomment-1445417280 https://github.com/erosman/support/issues/429#issuecomment-1445448982 https://github.com/erosman/support/issues/429#issuecomment-1456931722