1

its many days reading hundreds of ways to help me make what I really need. No success at all.

What I need is this:

1) Having a button which only works when the tab has a certain url.

2) After clicking it, must read page's source and then get some pieces of it to send them to my server page in order to check my database for recordcounts (I assume with AJAX & javascript). Then this page should send back to the extension its responses and populate the popup html.

Looks easy I know, but please I need the workflow if not the required codes for the extension.

Thank you so much!

1 Answers1

0

ok so you can chceck selected tab and it's url with:

chrome.tabs.getSelected(null,function(tab) {
  workWithUrl(tab.url);
});
...
function workWithUrl(url){
  if (url == ...
     ...
}

To be able to chceck this you need to add permission for "tabs"

To process page source code, send it to web service and change popup.html:

var xhr = new XMLHttpRequest();
xhr.open("POST", "__server adress___", true);
//headers   
xhr.setRequestHeader("Content-Type", "application/json");

//response 
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {

    //response from service to popup.html
    document.body.innerHTML = xhr.responseText;
    }
}

//process page here 

xhr.send(pageText);

You have to add permission for server adress to manifest as well and everything should be executed from popup.js (or html).

rhorvath
  • 3,525
  • 2
  • 23
  • 30
  • Thank you very much for your answer. Ill try this and will get back for the result :) –  Dec 06 '12 at 16:12
  • Even you thought I was asking how to get the source of a remote webpage using AJAX, it was the AJAX with which actually wanted to post in my server's database. Your code helped me very much to do so, the only thing I cant get right now is the tab's source. –  Dec 10 '12 at 01:15
  • Ok I found it from here http://stackoverflow.com/questions/11684454/getting-the-source-html-of-the-current-page-from-chrome-extension And with little alchemy I managed to merge it with the code you gave me :) Thank you again! –  Dec 11 '12 at 12:20
  • I have one more question. Could somebody else see the ajax part (aka the form field names Im sending to my server) and create his own addon in order to make posts to my server? –  Dec 12 '12 at 14:49
  • Nice to hear it helped, I am sorry I couldn't answer any sooner. If you are worried about somebody seeing your request to server, yes i think it is possible. Here are 2 ways i can think of now. 1 - using network analyzer like Wireshark and looking for POST/GET request you are making. 2 - reading the source code of your extension (if it is not obfuscated, it is pretty easy since it is stored in Chrome AppData on harddisk). – rhorvath Dec 12 '12 at 23:08
  • The things which should be hidden are the field names Im sending. I remember there was a primitive code like Ajax which was retrieving the cookie value the called dynamic page was creating. With this in mind I could first call a page setting cookies with the field names in them and after recieving as a response then use the ajax post method. But I cant remember that old code name... –  Dec 13 '12 at 00:34
  • Ok I found it http://www.remotescriptguru.com/RemoteScriptGuru/jsp/common/showSource.jsp?name=/src/rslite.js.txt This could dynamically send to my posting ajax part the field names, am I right? –  Dec 13 '12 at 00:52
  • I am not sure, but does it make it not traceable ? (if that was your intention - to hide posts going to server). – rhorvath Dec 13 '12 at 01:07
  • I dont claim myself a guru but take a look here http://www.ashleyit.com/rs/rslite/ Im sure you are more in position to answer if its traceable or not, even I was using it many times before ajax came up. –  Dec 13 '12 at 01:11
  • I don't think i can help with this remote scripting. But if you are afraid of somebody using your server, I would suggest you to try to code it, and than to use for example Wireshark to see if you are able to duplicate this kind of request (maybe it is a whole new question about security). But if the code responsible for httprequest remains the same as in my example, those http requests can be replicated even without extension. – rhorvath Dec 13 '12 at 01:33