1

i want to code a firefox extension where i can invite people to a "Yt-Music party" and it will sync up to the host YtMusic.

The element "movie_player" contains a lot of functions and variable which could be usefull, like the current time of the song.

For some reason the getCurrentTime() function works in the webconsole, but not when i have it like this, in my extension.

Do i need extra permission to do this?

Thanks in advance

const ytPlayer = document.getElementById('movie_player')

const test = ytPlayer.getCurrentTime()

console.log(test)

web console get element movie player

web console get current time

ImNuro
  • 21
  • 2

1 Answers1

0

Extensions don't have direct access to elements on the page. You'd have to inject a content script, and then pass data (via messaging) between the page and the extension's background script (or service worker) to have this type of communication. Note also that content scripts don't get access to things like ytPlayer by default either.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • It's not about communication but about [accessing the main world of the page](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts) because content scripts are isolated. For all we know from the incomplete description in the question the OP may already do it in a content script because it's also "in" extension. – wOxxOm Oct 06 '22 at 12:25
  • @wOxxOm Not sure what you mean by *isolated* here; content scripts are injected into the page (though they have a "clean view" of the environment). If you want to get the value from `getCurrentTime()`, you'd have to do that with a content script and pass it back (via messaging) to the background script. This is what I stated in my answer, so I'm not sure with which part you're disagreeing – Sampson Oct 06 '22 at 15:08
  • Your answer is non-working without the thing I mentioned. The most important difference about a content script is that it runs in the so-called "isolated world", it's a technical term in the extensions platform, which means a content script can't call getCurrentTime because it's not a built-in DOM method but a JS function added by the page. Firefox provides an unsafe `wrappedJSObject` trick to allow direct interaction, the article is linked in my comment above. – wOxxOm Oct 06 '22 at 15:35
  • @wOxxOm Nothing in my answer (or comment) says otherwise. The original question sounded as though the issue was with code ran from the context of a background script, rather than a content script (`wrappedJSObject` aside). That said, an explicit reference to `wrappedJSObject` is indeed a good addition. – Sampson Oct 06 '22 at 17:52
  • wrappedJSObject is a crucial piece of information omitting which makes the answer incomplete to a degree that it is essentially unusable as well as possibly completely unrelated in case the OP already uses a content script. – wOxxOm Oct 06 '22 at 19:31