0

Goal: show page info by using Youtube object that is defined in content script "Youtube.js"

Problem: can not access members.

I have the following script which is also registered in manifest.cs under content_scripts:

/// <reference path="UtilityFunctions.js" />
var playerElementId = "movie_player";
var youtube = new Youtube();

writePageInfo(youtube);

function Youtube() //ctor
{ 
   this.playerObj = getPlayer();
} 

Youtube.prototype.IsCurrentSite = function ()
{
  var url = location.href;

  if (url.indexOf("youtube.com") > 0)
  {
    return playerObj != null;
  }

  return false;
}

function getPlayer()
{
   return document.getElementById(playerElementId);
}

//debug
function writePageInfo(youtubeObj)
{
   //alert("From Youtube content script"); //this line is executed
   alert("Youtube - debug: " + youtubeObj.IsCurrentSite()); //this line NOT: undefined function
}

I do not know what to do to write the uncommented alert message.

P.S.: if I open several Youtube pages several objects will be created ?

1 Answer 1

1

I don't know what you are doing but the reason for the error is that you are creating and using an instance of Youtube constructor and then you are adding a method to its prototype. This new method (i.e. IsCurrentSite) does not exists when you pass the youtube object to writePageInfo function.

To fix this, you need to call writePageInfo(youtube) AFTER adding the method to the prototype:

var youtube = new Youtube();
Youtube.prototype.IsCurrentSite = function ()
{
  var url = location.href;

  if (url.indexOf("youtube.com") > 0)
  {
    return playerObj != null;
  }

  return false;
}

writePageInfo(youtube); //moved here

Answering your question, each browser tab (or window) has its own JavaScript environment.

Sign up to request clarification or add additional context in comments.

1 Comment

Conceptually at least, it's better to move it all the way up. You will then define how an object behaves before creating an instance of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.