Is there a way of getting the value of the title of the page from a Google Extension?
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 ;)
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;
// ...
});
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 ;)