views:

136

answers:

3

Is there a way of getting the value of the title of the page from a Google Extension?

A: 

It cannot be done with pure HTML, if you use apsx for it and aspx has been defined thusly:

<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/ATS06.Master" CodeBehind="AcceptBox.aspx.vb" Inherits="ATS06.AcceptBox" 
        title="ATS Accept Box" %>

then MethodCalled(Me.ContentPlaceHolder1.Page.Title) will pass in the page title.

I assume that without master page it is just MethodCalled(Me.Page.Title)

Then the MethodCalled needs to be defined as:

MethodCalled(ByVal sTitle As String)

If you cannot use aspx for this task post again I will try to write something in Javascript ;)

Cninroh
This is about a Google Chrome extension that runs within the browser.
Hannson
+4  A: 

At first you should declare the tabs API permission in your manifest.json:

{
  "name": "My extension",
  ...
  "permissions": ["tabs"],
  ...
}

Then you will be able to use the tabs API, you are looking for the chrome.tabs.getSelected(windowId, callback) method.

To get the selected tab of the current window, you can simply pass null as the windowId.

This method will execute the callback function passing a Tab object as its first argument, where you can simply get the title property:

chrome.tabs.getSelected(null,function(tab) { // null defaults to current window
  var title = tab.title;
  // ...
});
CMS
Works beautifully
Blondie
A: 

You can do it with pure Javascript :

var t = document.getElementsByTagName('title')[0];
if ( !!t.childNodes.length ) {
alert( t.firstChild.data );
} else if ( t.innerHTML ) {
alert( t.innerHTML );
}

but it will not work correctly in IE, let me explain why. This method will return a reference to the title element, but not the actual element itself. IE refuses to believe that there are any nodes inside the title element or its reference so it will not pass it. But it will return a value for innerHTML, so there might be glitches, you need to test it ;)

Cninroh
The OP is working on a [Google Chrome Extension](http://code.google.com/chrome/extensions/), and if you want to get the title of a page, you can simply use the [DOM Level 2](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-18446827) standard [`document.title`](https://developer.mozilla.org/en/DOM/document.title) property...
CMS