PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/utils/hydraload/chrome/content/hydraload/hydraloadOverlay.js

https://gitlab.com/timofonic/hydranode
JavaScript | 106 lines | 70 code | 23 blank | 13 comment | 5 complexity | 5dcad7b211f58affcd72585a91f0ff4c MD5 | raw file
  1. var linkSubmitWindow;
  2. var host = "127.0.0.1";
  3. var port = "9999";
  4. var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  5. if (prefs.getPrefType("hydraload.host") == prefs.PREF_STRING) {
  6. host = prefs.getCharPref("hydraload.host");
  7. }
  8. if (prefs.getPrefType("hydraload.port") == prefs.PREF_INT) {
  9. port = prefs.getIntPref("hydraload.port");
  10. }
  11. function tryDownload() {
  12. focused = document.commandDispatcher.focusedElement;
  13. //alert("DEBUG: tagName=" + focused.tagName);
  14. switch (focused.tagName) {
  15. //currently only links are supported...
  16. case "A":
  17. link = focused.toString();
  18. startDownload(link);
  19. //linkSubmitWindow = window.open(
  20. // "chrome://hydraload/content/linkSubmitWindow.xul",
  21. // "linkSubmitWindow",
  22. // "chrome, centerscreen"
  23. //);
  24. break;
  25. default:
  26. alert("Please right-click over a link...");
  27. }
  28. }
  29. function startDownload(link) {
  30. doRequest("modprobe http");
  31. doRequest("do " + link);
  32. }
  33. function createSubmitWindow(win) {
  34. text = "Trying to download: " + link;
  35. info = linkSubmitWindow.document.getElementById("submit-text");
  36. info.setAttribute("value", text);
  37. }
  38. function parseInputData(data) {
  39. //TODO: really parse the data ;-)
  40. //response = linkSubmitWindow.document.getElementById("submit-response");
  41. //response.setAttribute("value", data);
  42. }
  43. function doRequest(request) {
  44. var listener = {
  45. finished : function(data) {
  46. parseInputData(data);
  47. }
  48. }
  49. request += " \r\n";
  50. //alert("Host: " + host);
  51. //alert("Port: " + port);
  52. //alert("Request: " + request);
  53. sendData(host, port, request, listener);
  54. }
  55. function sendData(h, p, outputData, listener) {
  56. try {
  57. var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
  58. var transport = transportService.createTransport(null, 0, h, p, null);
  59. var outstream = transport.openOutputStream(0, 0, 0);
  60. outstream.write(outputData, outputData.length);
  61. var stream = transport.openInputStream(0, 0, 0);
  62. var instream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  63. instream.init(stream);
  64. var dataListener = {
  65. data : "",
  66. onStartRequest: function(request, context) {},
  67. onStopRequest: function(request, context, status) {
  68. instream.close();
  69. outstream.close();
  70. listener.finished(this.data);
  71. },
  72. onDataAvailable: function(request, context, inputStream, offset, count) {
  73. this.data += instream.read(count);
  74. },
  75. };
  76. var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
  77. pump.init(stream, -1, -1, 0, 0, false);
  78. pump.asyncRead(dataListener, null);
  79. } catch (ex) {
  80. return ex;
  81. }
  82. return null;
  83. }