質問

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 ?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top