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!
Education is a progressive discovery of our own ignorance.
ReplyDeleteI just get all worked up , and I stew around!
ReplyDeleteHi, 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.)
ReplyDeleteI'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.
Not intended as a comment: "JQuery" -> "jQuery". Ref: http://en.wikipedia.org/wiki/JQuery
ReplyDelete@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.
ReplyDeleteMerry Christmas, and God bless all of you, all of you on the good Earth.
ReplyDeleteIt's a nice place to share my thoughts. I'm willing to stick around here and watch it grow.
ReplyDeletewould 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().
ReplyDeleteThe less you stuff in it, the less you'll have to deal with if your wallet is lost or stolen.
ReplyDeleteEO EO EO EO ..... AND I DON'T KONW FIND MY STAR....SHINING STAR LIKE YOU......
ReplyDeleteThe other half is listening and understanding what others communicate to us.
ReplyDeleteOP is a laudy mama, yum
ReplyDeleteEven a baby gets fussy when his mother is upset!
ReplyDeleteWeblogs 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.
ReplyDeleteyou can find what you want to in here,make your life full of color ,it's a perfect website.
ReplyDeletesenin hayatını renk dolu olun, burada istediğiniz bulabilirsiniz, mükemmel bir web sitesinin. rt
ReplyDeleteI always learn a lot at these presentations from the questions people ask.
ReplyDeleteI resolve to achieve more in the new year
ReplyDeleteLove is in the winter of a sunshine, make the person feels cold and hunger nazr Mohammed human warmth.
ReplyDeleteInteresting post. I have been wondering about this issue,so thanks for posting.
ReplyDeleteThis is very interesting post and i think blog owner has done great work,i hope to see you more articles.
ReplyDeleteThanks, Sara? for your blogging.
ReplyDeleteRather informative and interesting
That is an awfully astounding column you’ve posted.keep this good working.
ReplyDeleteGreat tips but a bit hard for a newbie hehe!
ReplyDeleteI can't believe how much of this I just wasn't aware of. Thank you for bringing more information to this topic for me.
ReplyDeleteI can't believe how much of this I just wasn't aware of. Thank you for bringing information to me.
ReplyDeleteThe 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.
ReplyDeleteLittle 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.
ReplyDeleteI just added your web site to my favorites. I enjoy reading your posts. Thank you!
ReplyDeleteA burden of one's choice is not felt.
ReplyDeletevery good!
ReplyDeleteIt'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/
ReplyDeleteThank you so much! It saved a lot of my time ^^. You are the best
ReplyDelete