PageRenderTime 69ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/content/host_modules/myspace.js

https://github.com/websurfer/ImageHost-Grabber
JavaScript | 167 lines | 72 code | 26 blank | 69 comment | 8 complexity | 6c1b64dd7a26e1ae38a59af98b8f4faa MD5 | raw file
  1. /****************************** Start of GPL Block ****************************
  2. * ImageHost Grabber - Imagegrabber is a firefox extension designed to
  3. * download pictures from image hosts such as imagevenue, imagebeaver, and
  4. * others (see help file for a full list of supported hosts).
  5. *
  6. * Copyright (C) 2007 Matthew McMullen.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *************************** End of GPL Block *******************************/
  23. ihg_Functions.escape_everything = function escape_everything(sometext) {
  24. var tmp_string = escape(sometext);
  25. tmp_string = tmp_string.replace(/\*/g, "%2A");
  26. tmp_string = tmp_string.replace(/@/g, "%40");
  27. //tmp_string = tmp_string.replace(/-/g, "%2D");
  28. tmp_string = tmp_string.replace(/_/g, "%5F");
  29. tmp_string = tmp_string.replace(/\+/g, "%2B");
  30. tmp_string = tmp_string.replace(/\./g, "%2E");
  31. tmp_string = tmp_string.replace(/\//g, "%2F");
  32. return tmp_string;
  33. }
  34. ihg_Functions.get_cookies = function get_cookies() {
  35. var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
  36. var enumeratorThing = cookieManager.enumerator;
  37. var cookieTree = new Array();
  38. var count = 0;
  39. while(enumeratorThing.hasMoreElements()) {
  40. var newCookie = enumeratorThing.getNext().QueryInterface(Components.interfaces.nsICookie);
  41. if (newCookie.host.match(/^\.myspace/)) {
  42. cookieTree[count] = newCookie;
  43. count++;
  44. }
  45. }
  46. var CookieString = "";
  47. for (var i = 0; i < cookieTree.length; i++) {
  48. if (cookieTree[i].host.match(/^\.myspace/)) {
  49. CookieString += cookieTree[i].name + "=" + cookieTree[i].value + (i==cookieTree.length-1?"":"; ");
  50. }
  51. }
  52. return CookieString;
  53. }
  54. ihg_Functions.getMyspacePage = function getMyspacePage(pageURL, newEventTarget, newEventArgument) {
  55. if (!newEventTarget) var newEventTarget = 'ctl00$Main$PagerTop';
  56. var ref_url = pageURL;
  57. var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  58. var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  59. var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
  60. var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
  61. req = new XMLHttpRequest();
  62. req.open("GET", ref_url, false);
  63. req.send(null);
  64. CookieString = get_cookies();
  65. var theForm = req.responseText.match(/<form.+?id=("|')aspnetForm\1.*?>(?:.|[\f\n\r])+?<\/form>/)[0];
  66. var inputs = theForm.match(/<input.*>/g);
  67. var postData = "";
  68. for(var i = 0; i < inputs.length; i++) {
  69. var id = inputs[i].match(/id=("|')(.*?)\1/)[2];
  70. if(id.match(/EVENTTARGET/)) var value = newEventTarget;
  71. else if(id.match(/EVENTARGUMENT/)) var value = newEventArgument;
  72. else var value = inputs[i].match(/value=("|')(.*?)\1/)[2];
  73. if(id.match(/^ctl00/)) id = id.replace(/_/g, '%24');
  74. value = ihg_Functions.escape_everything(value);
  75. postData += id + "=" + value + (i==inputs.length-1?"":"&");
  76. }
  77. var ref_uri = ios.newURI(ref_url, null, null);
  78. ref_uri = ref_uri.QueryInterface(Components.interfaces.nsIURL);
  79. var cont_len = postData.length;
  80. var host = ref_uri.host;
  81. var file_and_args = ref_uri.path;
  82. var temp_thing = "POST " + file_and_args + " HTTP/1.1\r\n";
  83. temp_thing += "Accept-Encoding: gzip, deflate\r\n";
  84. temp_thing += "Host: " + host + "\r\n";
  85. temp_thing += "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6 XPCOMViewer/0.9.5\r\n";
  86. temp_thing += "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
  87. temp_thing += "Accept-Language: en-us,en;q=0.5\r\n";
  88. temp_thing += "---------------: ------------\r\n";
  89. temp_thing += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
  90. //temp_thing += "Keep-Alive: 300\r\n";
  91. temp_thing += "Connection: close\r\n";
  92. temp_thing += "Referer: " + ref_url + "\r\n";
  93. temp_thing += "Cookie: " + CookieString + "\r\n";
  94. temp_thing += "Content-Type: application/x-www-form-urlencoded\r\n";
  95. temp_thing += "Content-Length: " + cont_len + "\r\n\r\n";
  96. temp_thing += postData + "\r\n";
  97. var transport = transportService.createTransport(null,0,host,80,null);
  98. var outstream = transport.openOutputStream(0,0,0);
  99. outstream.write(temp_thing,temp_thing.length);
  100. var instream = transport.openInputStream(0,0,0);
  101. sis.init(instream);
  102. var dataListener = {
  103. responseText : "",
  104. onStartRequest : function() {},
  105. onStopRequest : function() {
  106. instream.close();
  107. outstream.close();
  108. dump("We did it!\n");
  109. var tmpObj = {
  110. target : {
  111. channel : { name : threadURL + "&page=" + newEventArgument },
  112. responseText : this.responseText
  113. }
  114. }
  115. getRDun(tmpObj);
  116. },
  117. onDataAvailable : function(request, context, inputStream, offset, count) {
  118. var poop = sis.read(count);
  119. this.responseText += poop;
  120. dump("count: " + count + "\n");
  121. dump("isPending: " + request.isPending() + "\n");
  122. }
  123. }
  124. pump.init(instream, -1, -1, 0, 0, false);
  125. pump.asyncRead(dataListener, null);
  126. }