0

I'm building a simple Chrome Extension to get the URL of each of the users browser pages when opened and send back to the server. I can currently get the URL to appear in an alert box, however my ajax is returning a blank value to my database. The alerts work fine though(?)

Any ideas/thoughts? See script below..

Popup.html:

<html>
    <head>
       <title>Facebook Connect For Chrome Extension</title>
       <script type="text/javascript" src="index.js"></script>
    </head>
    <body>
        <h1>Facebook Connect For Chrome Extension</h1>
        <p><a target="_blank"href="https://www.facebook.com/dialog/oauth?client_id=MY-APP-   ID&response_type=token&scope=email&redirect_uri=http://www.facebook.com">Facebook Connect</a></p>

    </body>
</html>

UPDATED background.js:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo != undefined) {
var http = new XMLHttpRequest(); 
  http.open("POST",
  "http://ec2-52-89-93-50.us-west-2.compute.amazonaws.com/phpindex.php", 
   true);
  sendme = changeInfo.url
   http.send(sendme);
}
alert(sendme);
}); 

UPDATED manifest.json:

{ "name": "Example", "version": "1.0", "description": "Example"

"browser_action": {
"default_popup": "popup.html",
"default_icon":"icon.png"
 },

"background": {
"scripts": ["background.js"]
},
"manifest_version":2,
"permissions": [
"tabs",
"http://*.facebook.com/*",
"http://MY_SERVER/*",
"http://*/*",
"https://*/*",
"<all_urls>"
]
}
  • You `manifest.json` doesn't include `background.js`. and `chrome.tabs.onUpdated.addListener` is enough. you don't need others. – Sungguk Lim Nov 02 '15 at 11:02
  • I had just left it out when posting on here, I've amended the code above. still doesn't seem to be working. Just get a blank inputs in MySQL database – PickMeUpLittle Nov 02 '15 at 11:14
  • I strongly suggest removing `chrome.tabs.onCreated.addListener` and `chrome.tabs.onActivated.addListener`. it's not necessary. So, with above code, you can receive callback when tab's url is updated? – Sungguk Lim Nov 02 '15 at 11:19
  • I've deleted the other code in background.js, leaving chrome.tabs.onUpdated.addListener. When i test I get 3 alerts, the first has the correct URL, the second and third just have 'undefined' – PickMeUpLittle Nov 02 '15 at 11:22

1 Answers1

0

As per tabs.onActivated documentation:

Note that the tab's URL may not be set at the time this event fired, but you can listen to onUpdated events to be notified when a URL is set.

Use chrome.tabs.onUpdated listener callback to post the url to your server.

wOxxOm
  • 43,497
  • 7
  • 75
  • 96
  • Thanks - bizarre I've updated my background.js yet still isn't picking it up? – PickMeUpLittle Nov 01 '15 at 18:37
  • 1. Make sure to reload the extension 2. Use the debugger – wOxxOm Nov 01 '15 at 18:47
  • Thanks, this hasn't really helped. I have used debugger and of course reloaded the extension. Potentially could this be due to restrictions for background pages? E.g versus using a content page? – PickMeUpLittle Nov 02 '15 at 08:30
  • Well, you've switched to `tabs.onCreated` which has the same problem as `tabs.onActivated` (see the documentation), however both the documentation and the answer were advising `tabs.onUpdated`. – wOxxOm Nov 02 '15 at 08:35
  • I've changed my background.js file.. But still receiving blank inputs in the database.. Could it be a restriction on background scripts? – PickMeUpLittle Nov 02 '15 at 11:15
  • Do you see the correct url in `alert(changeInfo.url);`? – wOxxOm Nov 02 '15 at 11:17
  • I've deleted the other code in background.js, leaving chrome.tabs.onUpdated.addListener. When i test I get 3 alerts, the first has the correct URL, the second and third just have 'undefined'. – PickMeUpLittle Nov 02 '15 at 11:20
  • Then you should check if the url is defined and only post it to your server in that case. – wOxxOm Nov 02 '15 at 11:23
  • I've updated code to check for undefined, this sends to the server and is still showing a blank input – PickMeUpLittle Nov 02 '15 at 11:27
  • Maybe there's something's wrong with the database configuration? Check by running `var http = new XMLHttpRequest(); http.open("POST", "http://MY-SERVER.php", true); http.send("test");` from the console of the background page. – wOxxOm Nov 02 '15 at 11:30
  • Console is returning – PickMeUpLittle Nov 02 '15 at 11:37
  • Which means there's a problem with XHR. Usually the data is sent to php in the form of `param=value` ([example](http://stackoverflow.com/a/15312976/3959875)). – wOxxOm Nov 02 '15 at 11:40
  • Updated the code, still not sending. This is very unusual :/ – PickMeUpLittle Nov 02 '15 at 11:51
  • The unusual thing in your code is the lack of the standard `param=value` notation in `send` and no `Content-type` and/or `Content-length` headers as shown in the example. – wOxxOm Nov 02 '15 at 11:53
  • `sendme = changeInfo.url` is not the same as `send("param=" + encodeURIComponent(changeInfo.url))` where of course `param` should be the real param name from your php code. – wOxxOm Nov 02 '15 at 11:56