0

In my content script, I call html element. This element have some defined methods (written in script of that page).

In my content script, I can get all behavior and some methods (DOM element) of that html element (via getElementById method), but I cannot call its defined methods. How can I do?

For example, on page http://example.com/test.html, there is an element with id is "xyz". This element have some methods doit, playit etc. These methods are written on script of this page.

In content script, I use:

var ob = document.getElementById("xyz");
ob.doit();//error 
Freelancer
  • 837
  • 6
  • 21
  • Either `ob` is `undefined`, or you havent defined `doit` on `Element`. [mcve] please! – Jonas Wilms Apr 02 '18 at 17:25
  • I can call behavior and dom methods of ob. But cannot use methods that were defined by script on page. – Freelancer Apr 02 '18 at 17:31
  • Can you provide an example of how you are including the functions? Generally, functions aren't directly bound to DOM elements, so there may be something weird going on. – samanime Apr 02 '18 at 17:31
  • 1
    `ob.doit();//error` So the error message you're getting is `error`, right? Well I'd have thought Google provides more information when errors occur. – connexo Apr 02 '18 at 17:36
  • Exactly - doit is not a function. I can call above code without error in debugger of web page, but cannot call in content script. – Freelancer Apr 02 '18 at 17:38
  • 1
    Closely related (but maybe different enough not to be duplicate): [Is it possible to inject a javascript code that OVERRIDES the one existing in a DOM?](https://stackoverflow.com/q/12095924/710446) (Not quite a duplicate because this question asks about how a content script can read a property from the main page, while that question asks about how a content script can *set* a property that the main page can read. The underlying problem of execution environments is the same, but the solutions are different.) – apsillers Apr 02 '18 at 17:38
  • Yeah, this article is helpful. Impossible in my case, because they run on separate environments. So, need choose another method (add script tag). – Freelancer Apr 02 '18 at 17:44
  • Possible duplicate of [Insert code into the page context using a content script](https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script) – wOxxOm Apr 03 '18 at 02:38

0 Answers0