Get title of the newly create tab

668 views
Skip to first unread message

Peter

unread,
Aug 14, 2010, 1:43:38 PM8/14/10
to Chromium-extensions
Hi,

I'm pretty new to the Chrome extensions and I'm having trouble with
getting the title of my newly created tab. Let me first post my
background.html

<html>
<head>
<script>
chrome.extension.onRequest.addListener(
function(url, sender, sendResponse){
chrome.tabs.create({url: url}, function(tab){
chrome.tabs.sendRequest(sender.tab.id, tab.title);
});
sendResponse();
}
);
</script>
</head>
</html>

So what I need is when my tab is created, the tab title is send back.
The sending back works, but the tab.title is null or empty. So I'm
assuming that this is because their hasn't anything loaded.

So can anyone help me out with this. I want to send the tab title as
soon as possible to my content script. How can I do this as fast as
possible? I also tried with executescript and by using document.title
but this only gives me the tab title from as soon the whole page is
loaded, but I want it as fast as possible.

So if anyone could help me out on this, it would be great.

PhistucK

unread,
Aug 14, 2010, 2:14:18 PM8/14/10
to Peter, Chromium-extensions
You need to change this code a bit -
<html>
<head>
<script>
function GetPageTitle(Response)
{
 alert(Response.Title);
}
chrome.extension.onRequest.addListener(
       function(request, sender, sendResponse){
               chrome.tabs.create({url: request.url}, function(tab){
//                       chrome.tabs.sendRequest(sender.tab.id, tab.title); // Drop this line.
                         chrome.tabs.sendRequest(sender.tab.id, {Action: "send-back-page-title"}, GetPageTitle); 

               });
               sendResponse();
       }
);
</script>
</head>
</html>

And you need a content script that matches the URL of that created tab (http://*/* for every page) that will have this code -
chrome.extension.onRequest.addListener(function(Request, Sender, Respond) {
 if (Request.Action == "send-back-page-title")
  Respond({Title: document.title});
})

☆PhistucK



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Peter

unread,
Aug 14, 2010, 2:33:31 PM8/14/10
to Chromium-extensions
I don't understand what you are doing here. Maybe didn't I explain
well what I want. The chrome.tabs.sendRequest(sender.tab.id,
tab.title); should send the title (of the newly created tab) back to
the content script it came from. But you are telling me that I need to
have a content script for every url that will be created in a new tab?
That wouldn't be very efficient since the url can be random and there
could be millions of different content scripts that need to made?

So do I don't understand what you are saying or is there a more
efficient way?
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .

PhistucK

unread,
Aug 14, 2010, 3:48:29 PM8/14/10
to Peter, Chromium-extensions
Why? I wrote a general function that sends back the title of the new tab.
And, apparently, you did not explain yourself well, because you said the code you showed in your first post is in the background page (not in the content script), if I understand correctly.

☆PhistucK


To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Peter

unread,
Aug 16, 2010, 9:42:40 AM8/16/10
to Chromium-extensions
I got it to work to get the title on the page that was already loaded.
Your script was indeed as how it should be, however when I changed it
to get the title of newly created tab, I get an empty alert. So the
question is why?

I execute a javascript code with

if (Request.Action == "send-back-page-title")
Respond({Title: document.title});

});

and let it callback, so it works but the title is empty.

So I'm back to the problem like stated in the first post. The title is
probably empty because the page hasn't been loaded fully or loaded
partially. How could I let it know it needs to try to get the title
until it isn't empty?

On Aug 14, 9:48 pm, PhistucK <phist...@gmail.com> wrote:
> Why? I wrote a general function that sends back the title of the new tab.
> And, apparently, you did not explain yourself well, because you said the
> code you showed in your first post is in the background page (not in the
> content script), if I understand correctly.
>
> ☆PhistucK
>
> > <chromium-extensions%2Bunsubscr...@chromium.org<chromium-extensions%252Bunsubscr...@chromium.org>

Peter

unread,
Aug 16, 2010, 10:00:43 AM8/16/10
to Chromium-extensions
I forgot to mention that letting the url callback succeeds, so I
definitely has something to do with the page not fully/partially
loaded. So how could I fix this? I have been thinking about a timer,
but that isn't good, because some pages can take ages for they begin
to load.

PhistucK

unread,
Aug 16, 2010, 10:23:15 AM8/16/10
to Peter, Chromium-extensions
You cannot access the document this way. The only part of the extension system that can access the document of a web page, is a content script. That is why I suggested that in the callback function of the tab creation call, you should a message (sendRequest) to that tab, so the content script will respond with the title. Alternatively, you can inject a content script dynamically, using chrome.tabs.executeScript and have it send a message to the background page.

☆PhistucK


To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Peter

unread,
Aug 16, 2010, 10:41:48 AM8/16/10
to Chromium-extensions
Well that is exactly what I have come up with, but there is a problem
with how my background page can see the difference between my
sendrequest with an url and a sendrequest with my title?

So my background page is:

<html>
<head>
<script>
chrome.extension.onRequest.addListener(
function(url, sender, sendResponse){
chrome.tabs.create({url: url}, function(tab){
chrome.tabs.executeScript(tab.id, {file: "pagechecker.js"});
});
sendResponse();
}
);
</script>
</head>
</html>

And the pagechecker.js:

var title = document.title;

if(document.title != ""){
chrome.extension.sendRequest(title);
}

But then it will open up a new tab again, because it think it is a
sendrequest with a url instead of title. And for full clarification
this is how the content script in the original tab sends the url:

chrome.extension.sendRequest("http://" + url);

So I could do a javascript match function to see if it is a url or
not, but I assume this is not the way you should do this?

On Aug 16, 4:23 pm, PhistucK <phist...@gmail.com> wrote:
> You cannot access the document this way. The only part of the extension
> system that can access the document of a web page, is a content script. That
> is why I suggested that in the callback function of the tab creation call,
> you should a message (sendRequest) to that tab, so the content script will
> respond with the title. Alternatively, you can inject a content script
> dynamically, using chrome.tabs.executeScript and have it send a message to
> the background page.
>
> ☆PhistucK
>
> > > > <chromium-extensions%2Bunsubscr...@chromium.org<chromium-extensions%252Bunsubscr...@chromium.org>
> > <chromium-extensions%252Bunsubscr...@chromium.org<chromium-extensions%25252Bunsubscr...@chromium.org>

Peter

unread,
Aug 16, 2010, 10:48:58 AM8/16/10
to Chromium-extensions
Or it would be even better if the executed content script could send
it over to the content script in the original tab, but I couldn't find
anything about that on the chrome howto page and on google. So I
assume from content script to content script from different tabs isn't
possible.

Ben

unread,
Aug 16, 2010, 11:06:32 AM8/16/10
to Peter, Chromium-extensions
You could do this (see below):

On Mon, 2010-08-16 at 07:41 -0700, Peter wrote:
> Well that is exactly what I have come up with, but there is a problem
> with how my background page can see the difference between my
> sendrequest with an url and a sendrequest with my title?
>
> So my background page is:
>
> <html>
> <head>
> <script>
> chrome.extension.onRequest.addListener(
> function(url, sender, sendResponse){
> chrome.tabs.create({url: url}, function(tab){
> chrome.tabs.executeScript(tab.id, {file: "pagechecker.js"});
> });
> sendResponse();
> }
> );
> </script>
> </head>
> </html>

Change the background page JS to:
chrome.extension.onRequest.addListener(
function(url, sender, sendResponse){
// you might consider changing url to
// something else in the above function
if (url.title) {
// do something with url.title
} else if (url.url) {
// do something with url.uri
}
sendResponse();
}
);

>
> And the pagechecker.js:
>
> var title = document.title;
>
> if(document.title != ""){
> chrome.extension.sendRequest(title);
> }
>

Change this to:

var title = document.title;

if (document.title != "") {
chrome.extension.sendRequest({'title':title})
// sends the value of title in an
// object attribute named 'title'
} else {
// do something else, for instance...
chrome.extension.sendRequest({'url':document.url})
// sends the full url of the current document
// in an object attribute named 'url'.
}

> But then it will open up a new tab again, because it think it is a
> sendrequest with a url instead of title. And for full clarification
> this is how the content script in the original tab sends the url:
>
> chrome.extension.sendRequest("http://" + url);
>
> So I could do a javascript match function to see if it is a url or
> not, but I assume this is not the way you should do this?
>

You could do that, but there are some cases where a Web page sets the
title to the URL of the page, which would cause your script to function
in unexpected ways. It'd probably be best to avoid that possibility
altogether.

--
Ben

alex

unread,
Aug 16, 2010, 2:39:52 PM8/16/10
to Chromium-extensions
var turl = document.URL;
chrome.extension.sendRequest(
{word: turl }
//,function(response) {
//console.log("###Get rsp"+response.farewell);
//}
);

Peter

unread,
Aug 16, 2010, 6:55:33 PM8/16/10
to Chromium-extensions
First of all thanks with help I got. I got it working now. There is
only one thing left I want to ask. Like already mentioned, I get the
title of page only when the page is loaded. Now, I want to know if
this can be faster? If you load a page and press "ESC" to stop the
page from loading, there already is a <title> attrube in the source
code. How come the javascript only looks after it when the page is
loaded? Shouldn't javascript be working while the page is loading?

Anyone has an idea to speed things up a little bit?

PhistucK

unread,
Aug 18, 2010, 2:42:14 AM8/18/10
to Peter, Chromium-extensions
Add "run_at": "document_start" to your content script definition ("js": [...], "run_at": ...).
But then - you are not guaranteed to have the title already, so you will have to query until you do (if ever, so until onload fires, for example).

☆PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
Reply all
Reply to author
Forward
0 new messages