The My Opera forums have been replaced with forums.opera.com. Please head over there to discuss Opera's products and features

See the new Forums

got a error, doesn't get object

Forums » Dev.Opera » Opera Extensions Development Discussions

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

26. September 2011, 19:16:56

stefanvd

Posts: 69

got a error, doesn't get object

Hi,

When i open a html5 video page, i inject this script. But in the opera console log, it give me a error:
Event thread: DOMNodeInserted
Uncaught exception: TypeError: Cannot convert 'o' to object
But the code work great on Chrome and Safari.
How to fix this?

See source link:
http://turnoffthelights.googlecode.com/files/injected.js

Kind Regards,
Stefan

27. September 2011, 12:33:48

d4n3

Posts: 957

From a quick look at the source code, I think the problem is assuming that the global object in extensions is window.

"o" is set from a self-calling function to "this.ytCinema" and i assume "this" in that context is "window", since the initvideoinject function is called from a DOM event.

But, the declaration of ytCinema is done as an implicit global which gets set in the extension globals object (I forget what it's called, but it's not window) when the injected script runs.

So "o" is looking for window.ytCinema, but ytCinema is actually set in a different extension global context, so "o" gets set to undefined.

As a fix, you could try explicitly setting ytCinema on window on the first line:

(window.ytCinema = {


Disclaimer: haven't really tested any of this smile

29. September 2011, 17:28:58

stefanvd

Posts: 69

Thanks for you reply!
This make it now less error, but still got the error when i click on a play/pause button.
Example html5 video i use:
http://camendesign.com/code/video_for_everybody/test.html

This error still showing ->
Uncaught exception: TypeError: Cannot convert 'o' to object

Kind Regards,
Stefan

30. September 2011, 11:13:58

d4n3

Posts: 957

That error message isn't very helpful, as you didn't specify what line it happens on.

But, I'm guessing it might be that in a reference to "this.ytCinema", "this" is not set to "window".

I would rewrite the code to reference ytCinema in a local closure variable, like so:

(function(){
var ytCinema;

ytCinema = {
   players: {
...

        }(ytCinema, htmlplayer[j]));

...

        }(ytCinema));

...

};

ytCinema.initialize();

})();





This will get rid of the global namespace pollution and have a local reference to ytCinema.

30. September 2011, 11:51:25

stefanvd

Posts: 69

still got the problem, here the full error message i receive:

[30/09/2011 13:42:29] JavaScript - http://camendesign.com/code/video_for_everybody/test.html
Event thread: load
Uncaught exception: TypeError: Cannot convert 'o' to object
Error thrown at line 94, column 7 in <anonymous function: initialize>(o, p) in injected.js:
called from line 83, column 6 in <anonymous function: initialize>(players) in injected.js:
(function(o, p) {
called from line 99, column 4 in initvideoinject(e) in injected.js:
setPlayerEvents(htmlplayer);


And when i click on the pauze or play button of the video, i receive this:

[30/09/2011 13:43:29] JavaScript - http://camendesign.com/code/video_for_everybody/test.html
Event thread: pause
Uncaught exception: TypeError: Cannot convert 'o' to object
Error thrown at line 85, column 56 in <anonymous function: pause>() in injected.js:
o.players.active -= 1;



Kind Regards,
Stefan

3. October 2011, 06:39:20

d4n3

Posts: 957

You still get that error message, after you've passed in ytCinema from a local variable? I find that hard to believe.

Can you post your modified source code?

3. October 2011, 07:42:23

stefanvd

Posts: 69

Here is the source code (with change of the above suggestion):
http://turnoffthelights.googlecode.com/files/injected2.js

Kind Regards,
Stefan

3. October 2011, 09:10:08

d4n3

Posts: 957

I cannot reproduce.

I created a new extension that injects the file you linked. I tested this on the page you linked above (video for everybody). I confirmed that the file is injected, and runs without throwing exceptions. The play and pause events also run without exceptions.

I've set breakpoints on the lines where "o" is used and confirmed in the console that "o" does reference the "ytCinema" object.

Are you sure you are injecting the correct file? Do you have multiple versions of the extension installed?
Try setting up some breakpoints in dragonfly and stepping through the program.

3. October 2011, 09:17:37

stefanvd

Posts: 69

I refresh the extension, but got now this error:

[3/10/2011 11:11:53] JavaScript - http://camendesign.com/code/video_for_everybody/test.html
Script thread creation
Maximum script thread recursion depth exceeded
[3/10/2011 11:11:53] JavaScript - http://camendesign.com/code/video_for_everybody/test.html
Event thread: DOMNodeRemoved
Uncaught exception: DOMException: NOT_FOUND_ERR
Error thrown at line 48, column 3 in <anonymous function: playerStateChange>(stateId):
message.innerText = stateIO;
called from line 116, column 31 in <anonymous function: initialize>():
o.playerStateChange(0);


Kind Regards,
Stefan

3. October 2011, 10:42:17

d4n3

Posts: 957

I cannot reproduce the errors you specified above.

I assume that the problem you started this thread about (o not getting a reference to ytCinema) is now resolved?

It's really hard for me to help you as you don't provide enough information about what you are trying to do and you don't seem to want to try debugging your own code.

As for the errors, the first one seems to be caused by some sort of infinite recursion.

As for the second one, it looks like the problem is that
document.getElementById("ytCinemaMessage")

does not find that element as it probably doesn't exist. I don't see you adding that element in any part of your code, so where is this supposed to be created?

Once again, I would advise you to use dragonfly to set up breakpoints to step through your script and use the console or variable inspector to see if the code behaves as expected.

3. October 2011, 10:55:37

stefanvd

Posts: 69

It solve the 'o not getting a reference to ytCinema', but it create also a new error.
The <div>ytCinemaMessage</div> is created by a other javascript file.

The original file work great on Safari and Chrome. And if i remove the
"if (htmlplayer && htmlplayer.length > 0) { ..."
part of the code. Then my extension work great.

3. October 2011, 11:54:59

d4n3

Posts: 957

Originally posted by stefanvd:

It solve the 'o not getting a reference to ytCinema', but it create also a new error.


It didn't "create" a problem. It exposed a problem that was there from the start, but wasn't apparent since the script didn't execute that far due to the earlier exception.


var htmlplayer = document.getElementsByTagName("video") || false; 

htmlplayer will never be false here - getElementsByTagName returns a NodeList with length 0 if no elements are found, and that object is truthy (a regular empty array [] is also truthy, incase you expected a regular array here).

if (htmlplayer && htmlplayer.length > 0) { // HTML5 video elements 


So then, the only reason that this statement wouldn't execute is if the length is 0.

Set a breakpoint and step through it to see what happens and what causes the problem.


3. October 2011, 11:58:18

stefanvd

Posts: 69

Thanks for you help. smile
I will research it here again with breakpoint.

Forums » Dev.Opera » Opera Extensions Development Discussions