Thursday 18 April 2013

Chrome Extension SWF Page Requests for Edgeworld Meldown value (1)

So the most difficult part of working with the Edgeworld API was the fact you needed the meltdown value to initiate the session with Edgeworld in order to access the data.  I learned that in addition to that you needed to have an active session with a particular sector in order to download data from that sector even if the meltdown value was the same.  Finally, even if you were actively using the meltdown value to download data it would eventually time out. So I set of on a path to try to capture this value the answer was Chrome Extensions.  Now I assume you can do the same thing with other browsers but all I will focus on right now is Google Chrome.

You can browse through the Google Chrome Extension library on your own but I will work you through
chrome.devtools.network.onRequestFinished.addListener()
for the purposes of this post.

This post will be discussed in 2 parts.  This first part will be setting up the files required.  In part two I will try to explain what is happening and then we will install the extension.

So what you will need for this tutorial is to create a new folder called EdgeworldExtension.  Within that folder create 4 text files called devtools.html, devtools.js, background.jsmanifest.json.

devtools.html

<html>
  <head>
    <script src="devtools.js"></script>
  </head>
  <body>
  </body>
</html>

The purpose of this file seems to be just to call the javascript file so I am unsure of why it is required but at this time it is required so just build it.

devtools.js

function sendNote(myValue) {
   chrome.runtime.sendMessage({greeting: myValue}, function(response) {
     console.log(response.farewell);
   });
}

chrome.devtools.network.onRequestFinished.addListener(
    function(mydata) {
  var requestURL = mydata.request.url;
  var myRequest = requestURL.toString();
  
  if(requestURL.indexOf("meltdown") >-1)
  {
   var start=requestURL.indexOf("meltdown") + 8;
   var meltdownParam = requestURL.substring(start,requestURL.length);
   var end=meltdownParam.indexOf("&");
   var meltdown = meltdownParam.substring(1,end);

   console.log("Meltdown found");
   console.log(meltdown);
   sendNote(meltdown);
   
  }
 });


This is a content script file it is the bulk of the code, however, not all API calls can be utilized in this context. This is why we need the background page below. If you get the error
Uncaught TypeError: Cannot call method 'set' of undefined
it is most likely because you are trying to make an API call that is only available in the background pages.

function setMyCookie(myValue) {
 chrome.cookies.set({ url: "http://gamerprogress.com/", name: "edgeworldMeltdown", value: myValue });
}

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(request.greeting);
    sendResponse({farewell: "meltdown received - cookie set"});
 
 setMyCookie(request.greeting);
  });


This file is a background page that has access to the chrome.cookies.set that is not available in the content scripts as described above.


manifest.json


{
  "name": "EW Meltdown Grabber",
  "version": "1.0.2",
  "manifest_version": 2,
  "description": "Grabs the metldown value out of EW to be used in API calls.",
  "devtools_page": "devtools.html",
 "background": { "scripts": ["background.js"] },
 "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*",
 "cookies"
  ]
}


This is the file that sets the permissions for this extension as well as the title and the description.

No comments:

Post a Comment