Wednesday, January 5, 2011

Using JQuery AJAX Calls in a Chrome Plugin

Chrome really fights cross site scripting like the plague, whether you are building a plugin or not. Some of us love the simplicity of the JQuery AJAX functions and want to use them in our content scripts instead of the ugly XMLHttpRequests. If you throw one in, you will get a "Origin https://mail.google.com is not allowed by Access-Control-Allow-Origin" error.Use the code below to enable $.post, $.get, $.ajax, and $.post. 



In your background page, include the following JS. What this is doing is adding a listener for the partiular functions (you specify which in your request), and funneling the request through your jquery libarary (notice, we add it in the beginning). 



 








Then to the top of your content scripts, add these functions that work as proxies:




_getJSON = function(url, callback) {
console.log("calling JSON");
chrome.extension.sendRequest({action:'getJSON',url:url}, callback);
}
_ajax = function(url, callback, type, async) {
console.log("calling ajax");
chrome.extension.sendRequest({action:'ajax',url:url, type:type, async:async}, callback);
}

_get = function(url, callback) {
console.log("sending get");
chrome.extension.sendRequest({action:'get',url:url}, callback);
}

_post = function(url, data, callback) {
console.log("sending post");
chrome.extension.sendRequest({action:'post', data: data, url:url}, callback);
}



 



If you would like more of the functionality from the functions, you need to pass them from your content script. Add them as parameters in the proxy functions. 



That's it, enjoy plugin development!




33 comments:

  1. Education is a progressive discovery of our own ignorance.

    ReplyDelete
  2. I just get all worked up , and I stew around!

    ReplyDelete
  3. Hi, Sara. Cool, a fellow devchix (devchick?) working on browser extensions! I've been doing the same for the last several months. Actually as a recent convert to web development this was my first opportunity to use ajax, which I did similarly to you. (Not using jQuery in my extension, just the regular XHR.)
    I'm not clear on the error message you were trying to work around -- what does mail.google.com have to do with any of this? And are you saying you couldn't load jquery as a content script? Or unable to use the jQuery ajax methods in a content script? It seems you ought to be able to do both. On the other hand, you can only use ajax in content scripts for requests to the domain the host page is on, which wouldn't be all that useful in a browser extension anyway!
    How long have you been doing browser extensions? Do you find there's a large community of developers doing that? It seems to me it's kind of niche.

    ReplyDelete
  4. @Sharon great to meet you!!! I was trying to make a cross domain call from an extension using the jQuery ajax methods. That's where I was having trouble.

    ReplyDelete
  5. Merry Christmas, and God bless all of you, all of you on the good Earth.

    ReplyDelete
  6. It's a nice place to share my thoughts. I'm willing to stick around here and watch it grow.

    ReplyDelete
  7. would it not be easier to add the target URL, i.e. http://mail.google.com/ to the permissions section of the extension manifest.json? I'm sure that's worked for me in the past when using $.post().

    ReplyDelete
  8. The less you stuff in it, the less you'll have to deal with if your wallet is lost or stolen.

    ReplyDelete
  9. EO EO EO EO ..... AND I DON'T KONW FIND MY STAR....SHINING STAR LIKE YOU......

    ReplyDelete
  10. The other half is listening and understanding what others communicate to us.

    ReplyDelete
  11. Even a baby gets fussy when his mother is upset!

    ReplyDelete
  12. Weblogs may be a quite marketable and very rewarding instrument if employed correctly. Profiting from blogs is just a matter of grabbing the interest of an audience and never performing any actual salesmen promoting.

    ReplyDelete
  13. you can find what you want to in here,make your life full of color ,it's a perfect website.

    ReplyDelete
  14. senin hayatını renk dolu olun, burada istediğiniz bulabilirsiniz, mükemmel bir web sitesinin. rt

    ReplyDelete
  15. I always learn a lot at these presentations from the questions people ask.

    ReplyDelete
  16. I resolve to achieve more in the new year

    ReplyDelete
  17. Love is in the winter of a sunshine, make the person feels cold and hunger nazr Mohammed human warmth.

    ReplyDelete
  18. Interesting post. I have been wondering about this issue,so thanks for posting.

    ReplyDelete
  19. This is very interesting post and i think blog owner has done great work,i hope to see you more articles.

    ReplyDelete
  20. Thanks, Sara? for your blogging.
    Rather informative and interesting

    ReplyDelete
  21. That is an awfully astounding column you’ve posted.keep this good working.

    ReplyDelete
  22. Great tips but a bit hard for a newbie hehe!

    ReplyDelete
  23. I can't believe how much of this I just wasn't aware of. Thank you for bringing more information to this topic for me.

    ReplyDelete
  24. I can't believe how much of this I just wasn't aware of. Thank you for bringing information to me.

    ReplyDelete
  25. The game turned in the span of three plays in the third quarter. After Stanford scored to take a 19-12 lead, the Hokies looked poised to tie the game as they drove into Cardinal territory behind the efforts of quarterback Tyrod Taylor.

    ReplyDelete
  26. Little Miss Whiskey’s Golden Dollar presents a special four course food and ber pairing menu with Chef Graig Glufling or Liberty Tree tonight from 6pm to 8pm.

    ReplyDelete
  27. I just added your web site to my favorites. I enjoy reading your posts. Thank you!

    ReplyDelete
  28. It's a nice place to share my thoughts. I'm willing to stick around here and watch it grow. vd http://www.filmizled.com/film/yabanci-filmler/macera/

    ReplyDelete
  29. Thank you so much! It saved a lot of my time ^^. You are the best

    ReplyDelete