chrome.history.search API in Google chrome extension is not working

2,099 views
Skip to first unread message

jyothsna iyer

unread,
Apr 4, 2012, 1:21:25 AM4/4/12
to chromium-...@chromium.org
Hello,
I am a beginner in javascript.
We are trying to search the browsing history of the sites visited in chrome using chrome.history.search API.

This is the code we are trying to execute.

chrome.history.search({'stackoverflow.com ', startTime: 0},function(historyItems)
{alert("SJJ");
});

We have also added the permissions in the JSON script.
It does not alert the message which means there is an error in the syntax of search.

Regards,
Jyothsna

Sam Kerner

unread,
Apr 4, 2012, 10:08:05 AM4/4/12
to jyothsna iyer, chromium-...@chromium.org
On Wed, Apr 4, 2012 at 1:21 AM, jyothsna iyer <angel....@gmail.com> wrote:
> Hello,
> I am a beginner in javascript.
> We are trying to search the browsing history of the sites visited in chrome
> using chrome.history.search API.
>
> This is the code we are trying to execute.
>
> chrome.history.search({'stackoverflow.com ', startTime:
> 0},function(historyItems)
> {alert("SJJ");
> });
Jyothsna,
Take a look at the documentation for chrome.history.search():
http://code.google.com/chrome/extensions/history.html#method-search

The first parameter has no key named 'stackoverflow.com '. I think
you are trying to search for history items which include
'stackoverflow.com ', in which case you want something like this:

chrome.history.search( { text: "stackoverflow.com" }, function(historyItems) {
console.log("Got some history items");
});

console.log is a better choice than alert() for extension API
callbacks, because it doesn't block. See
http://code.google.com/chrome/extensions/tut_debugging.html for more
information on tools that can help you debug.

Sam


>
> We have also added the permissions in the JSON script.
> It does not alert the message which means there is an error in the syntax of
> search.
>
> Regards,
> Jyothsna
>

> --
> 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.

Sam Kerner

unread,
Apr 5, 2012, 12:45:30 AM4/5/12
to jyothsna iyer, Chromium-extensions
On Wed, Apr 4, 2012 at 8:49 PM, jyothsna iyer <angel....@gmail.com> wrote:
> Hi,
> Thanks for the response.
> I did whatever you have mentioned and also debugged the code referring to
> the tutorial but I get errors in the statement chrome.history.search.
> May be I am missing something:

Jyothsna,
I re-added the chrome extensions mailing list. When asking about
development issues, please keep the mailing list CCed. This has two
benefits. First, you are likely to get a better answer if a wider
audience sees your question. Second, the next person who runs into
the same issue will be able to find this discussion using a search
engine.


> content scripts has the name of the file myjs.js whose code should be
> activated when URL is entered.
> These are the exceptions I am getting
>
> Uncaught Error: "search" can only be used in extension processes. See the
> content scripts documentation for more details.

I think you are trying to use the chrome history API from a content
script. Content scripts can't use most extension APIs, including the
history API. You will need to create a background page, and have
javascript in the background page call the history API. See
http://code.google.com/chrome/extensions/background_pages.html for
information.

Content scripts give access to the DOM of a page. If you don't need
that, you might be able to write all your code in a background page.
If you do need DOM access, you will need to communicate between your
content script and your background page. See
http://code.google.com/chrome/extensions/messaging.html for an
explanation of how to do this.

Sam

jyothsna iyer

unread,
Apr 9, 2012, 12:31:47 AM4/9/12
to Sam Kerner, Chromium-extensions

Hello,
Thanks for your previous response.
Our requirement is as follows:
When a URL is entered ,the .js file in the content script gets activated and the URL needs to be compared with the history of the browser.
When we wrote history code in the content script it did not work , so we have written it in the .js file which is called from an html file ,but this .js file will not get the URL entered hence we are trying to communicate between the .js file in the content script and the.js which is called from the background.
We are facing issues in that.Could you please read below:

We are trying to implement message passing mechanism as follows:
1 js file(running as a content script) is trying to send document.URL to another js file(running as a popup background page ).
In the content Script js file we have the following code :
 
 chrome.extension.sendRequest({greeting: document.URL}, function(response) {
  alert("msg sent");
  console.log(response.farewell);
});


On the popup script we have the following listener :

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    
  
   alert("msg recvd");
  sendResponse({farewell: "received"});
  );


When this is executed we get 1 alert box - msg sent , but the receiver side does not get executed.

Sam Kerner

unread,
Apr 9, 2012, 10:48:48 AM4/9/12
to jyothsna iyer, Chromium-extensions
On Mon, Apr 9, 2012 at 12:31 AM, jyothsna iyer <angel....@gmail.com> wrote:
>
> We are trying to implement message passing mechanism as follows:
> 1 js file(running as a content script) is trying to send document.URL to
> another js file(running as a popup background page ).
> In the content Script js file we have the following code :
>
>  chrome.extension.sendRequest({greeting: document.URL}, function(response) {
>   alert("msg sent");
>   console.log(response.farewell);
> });
>
>
> On the popup script we have the following listener :
>
> chrome.extension.onRequest.addListener(
>   function(request, sender, sendResponse) {
>
>
>    alert("msg recvd");
>   sendResponse({farewell: "received"});
>   );
>
>
> When this is executed we get 1 alert box - msg sent , but the receiver side
> does not get executed.

If you are seeing the alert with "msg sent", then you got a response.
The second parameter to the function chrome.extension.sendRequest() is
a callback. This callback is run only when a response is received.
If a response was recieved, than it must have been sent, so I bet your
background page called sendResponse(). I think background pages do
not run alert(), so you will never see alert("msg recvd"); even if
your code works.

Try changing alert("msg sent"); to alert("Got a response: " +
JSON.stringify(response, null, 2)); to see the value of the response
parameter. I think you will find that response is the object you
passed to sendResponse: { farewell: "received" }.

If you want to see what the background page is doing, console.log() is
generally a better choice than alert(), because it works consistently
in tabs, popups, background pages, and so on.

Sam

jyothsna iyer

unread,
Apr 9, 2012, 9:53:37 PM4/9/12
to Chromium-extensions, Sam Kerner


Hello Sam,
Thanks for your earlier  response.We are getting "Could not establish connection.Receiving end does not exist."
We searched the net for solutions to this error and changed the manifest.json to have the .html file  in background page,initially it was in the browser action.

I tried as you said but I get a port error and it is not able to establish a connection.When I do 
 alert("Got a response: " +
JSON.stringify(response, null, 2)); 
I get 'undefined' value in the response.That is because the addListener function is not being called.
The sender code is in the content script[hash.js]
The listener code is in a .js file which is being called from an html background page.
The manifest file is as follows :

{
  "name": "My Second Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
    "content_scripts": [
   {
     "matches": ["https://login.yahoo.com/*"],
     
     "all_frames": true,
     "js": ["hash.js","2.5.3-crypto-sha256.js"]
    }
  ],
 "background_page": "src/popup.html",
  "browser_action": {
    "default_icon": "icon.png"

  },
  "permissions": ["history","tabs"]
 
}


Regards,
Jyothsna


On Mon, Apr 9, 2012 at 5:20 PM, jyothsna iyer <angel....@gmail.com> wrote:

Sam Kerner

unread,
Apr 10, 2012, 6:14:54 PM4/10/12
to jyothsna iyer, Chromium-extensions
On Mon, Apr 9, 2012 at 9:53 PM, jyothsna iyer <angel....@gmail.com> wrote:
>
>
> Hello Sam,
> Thanks for your earlier  response.We are getting "Could not establish
> connection.Receiving end does not exist."
> We searched the net for solutions to this error and changed the
> manifest.json to have the .html file  in background page,initially it was in
> the browser action.
>
> I tried as you said but I get a port error and it is not able to establish a
> connection.When I do
>  alert("Got a response: " +
> JSON.stringify(response, null, 2));
> I get 'undefined' value in the response.That is because the addListener
> function is not being called.
> The sender code is in the content script[hash.js]
> The listener code is in a .js file which is being called from an html
> background page.

Is the background page loading? If you go to chrome://extensions,
make sure the "Developer Mode" checkbox is checked, and click the
small grey triangle to the left of your extension, you should see a
list of active views. Is there a link to popup.html ?

If so, click it to open the debugger on your background page. Are
there any errors in the code that prevented the javascript code "
chrome.extension.onRequest.addListener( ..." from running?

Reply all
Reply to author
Forward
0 new messages