Search code examples
google-chromegoogle-chrome-extensionnpapicontent-script

Accessing DOM object properties from Chrome's content script


I ran into a strange problem with a content script. The content script is defined as "run_at" : "document_end" in the manifest. After a page is loaded the script inserts an object tag into the page (if the tag with predefined id does not exist yet), and sets some properties in it, such as type, width, height, innerHTML, and title. All works fine here.

function checkForObject()
{
  var obj = document.getElementById("unique_id");
  if(obj == null)
  {
    var d = document.createElement("object");
    d.id = "unique_id";
    d.width = "1";
    d.height = "1";
    d.type = "application/x-y-z";
    d.title = "1000";
    d.style.position = "absolute";
    d.style.left = "0px";
    d.style.top = "0px";
    d.style.zIndex = "1";
    document.getElementsByTagName("body")[0].appendChild(d);
  }
}

checkForObject();

I see the new object in the page html-code with proper values in its properties.

Some time later I need to read the title property of the object in the same content script. The code is simple:

function ReadTitle()
{
  var obj = document.getElementById("unique_id");
  var value = obj.title; // breakpoint
  console.log(value);
  // TODO: want to use proper title value here
}

The function is called from background.html page:

chrome.tabs.onActivated.addListener(
function(info)
{
  chrome.tabs.executeScript(info.tabId, {code: 'setTimeout(ReadTitle, 250);'});
});

Unfortunately, in ReadTitle I'm getting not what I expect. Instead of current value of the title I see the logged value is:

function title() { [native code] }

If I set a breakpoint at the line marked by // breakpoint comment, I see in the watcher that all object properties including the title are correct. Nevertheless, the variable value gets the abovementioned descriptive string.

Apparently, I have missed something simple, but I can't figure it out.

The answer. It was a bug in the npapi plugin, which hosts the object of used type. My apologies for all who have read the question with intention to help.


Solution

  • The NPAPI plugin used in the object erroneously reported title as supported method.