PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/chrome/scribefire/adbull.js

http://scribefire-chrome.googlecode.com/
JavaScript | 190 lines | 141 code | 47 blank | 2 comment | 23 complexity | f746bc4f6d1557ef7dbf95cf294c7271 MD5 | raw file
Possible License(s): LGPL-2.1
  1. var ADBULL = {
  2. load : function () {
  3. $(".ui-adbull").show();
  4. $("#button-blog-adbull").live("click", function (e) {
  5. e.preventDefault();
  6. ADBULL.addSite(SCRIBEFIRE.getBlog());
  7. });
  8. var style = $("<link/>");
  9. style.attr("rel", "stylesheet").attr("type", "text/css").attr("href", "skin/adbull.css");
  10. $("head:first").append(style);
  11. },
  12. api : {
  13. request : function (method, argString, callback) {
  14. var req = new XMLHttpRequest();
  15. var fullUrl = "http://www.chrisfinke.com/adbull/";
  16. if (method === "GET" && argString) {
  17. fullUrl += "?" + argString;
  18. }
  19. req.open(method, fullUrl, true);
  20. if (method === "POST") {
  21. req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  22. }
  23. req.onreadystatechange = function () {
  24. if (req.readyState == 4) {
  25. var text = req.responseText;
  26. try {
  27. var json = JSON.parse(text);
  28. } catch (invalidJSON) {
  29. alert(scribefire_string("error_adbull", [ text ]));
  30. return;
  31. }
  32. callback(json, req.status);
  33. }
  34. };
  35. if (method === "POST" && argString) {
  36. req.send(argString);
  37. }
  38. else {
  39. req.send(null);
  40. }
  41. }
  42. },
  43. showCode : function (code) {
  44. $(document).trigger("close.facebox");
  45. $("#panel-adbull-code .adbull_code").text(code);
  46. $.facebox($("#panel-adbull-code"));
  47. },
  48. addSite : function (site) {
  49. var blog = SCRIBEFIRE.getBlog();
  50. if (blog.url === site.url && "adbull_code" in blog) {
  51. ADBULL.showCode(blog.adbull_code);
  52. return;
  53. }
  54. if (SCRIBEFIRE.prefs.getCharPref("adbull.username") && SCRIBEFIRE.prefs.getCharPref("adbull.password")) {
  55. var panel = $("#panel-adbull-add");
  56. var form = $("#form-adbull-add");
  57. form.find("input[name='url']").val(site.url);
  58. form.die("submit").live("submit", function (e) { e.preventDefault(); });
  59. $("#button-adbull-add-continue").die("click").live("click", function (e) {
  60. e.preventDefault();
  61. form.find("p").removeClass("error");
  62. form.find(".error-message").hide();
  63. $("#button-adbull-add-continue").addClass("busy");
  64. var args = form.serialize();
  65. args += "&username=" + encodeURIComponent(SCRIBEFIRE.prefs.getCharPref("adbull.username"));
  66. args += "&password=" + encodeURIComponent(SCRIBEFIRE.prefs.getCharPref("adbull.password"));
  67. ADBULL.api.request("POST", args, function (data, status) {
  68. if (!data.status) {
  69. $("#button-adbull-add-continue").removeClass("busy");
  70. $(form.find(".error-message")).text(data.msg).show();
  71. if (data.field) {
  72. form.find("input[name='" + data.field + "']").closest("p").addClass("error");
  73. }
  74. }
  75. else {
  76. var url = form.find("input[name='url']:first").val();
  77. var argString = "a=code";
  78. argString += "&username=" + encodeURIComponent(SCRIBEFIRE.prefs.getCharPref("adbull.username"));
  79. argString += "&password=" + encodeURIComponent(SCRIBEFIRE.prefs.getCharPref("adbull.password"));
  80. argString += "&url=" + encodeURIComponent(url);
  81. // Get the embed code for this website.
  82. ADBULL.api.request("POST", argString, function (data, status) {
  83. if (data.status) {
  84. $("#button-adbull-add-continue").removeClass("busy");
  85. var blog = SCRIBEFIRE.getBlog();
  86. if (blog.url === url) {
  87. blog.adbull_code = data.data;
  88. SCRIBEFIRE.setBlog(blog);
  89. }
  90. ADBULL.showCode(data.data);
  91. }
  92. else {
  93. alert(data.msg);
  94. }
  95. });
  96. }
  97. });
  98. });
  99. $.facebox(panel);
  100. }
  101. else {
  102. ADBULL.register(site);
  103. }
  104. },
  105. register : function (site) {
  106. var panel = $("#panel-adbull-register");
  107. var form = $("#form-adbull-register");
  108. if (site) {
  109. form.find("input[name='url']").val(site.url);
  110. }
  111. form.die("submit").live("submit", function (e) { e.preventDefault(); });
  112. $("#button-adbull-register-continue").die("click").live("click", function (e) {
  113. e.preventDefault();
  114. form.find("p").removeClass("error");
  115. form.find(".error-message").hide();
  116. $("#button-adbull-register-continue").addClass("busy");
  117. ADBULL.api.request("POST", form.serialize(), function (data, status) {
  118. if (!data.status) {
  119. $("#button-adbull-register-continue").removeClass("busy");
  120. form.find(".error-message").text(data.msg).show();
  121. if (data.field) {
  122. form.find("input[name='" + data.field + "']").closest("p").addClass("error");
  123. }
  124. }
  125. else {
  126. SCRIBEFIRE.prefs.setCharPref("adbull.username", form.find("input[name='username']").val());
  127. SCRIBEFIRE.prefs.setCharPref("adbull.password", form.find("input[name='password']").val());
  128. var argString = "a=code";
  129. argString += "&username=" + encodeURIComponent(SCRIBEFIRE.prefs.getCharPref("adbull.username"));
  130. argString += "&password=" + encodeURIComponent(SCRIBEFIRE.prefs.getCharPref("adbull.password"));
  131. argString += "&url=" + encodeURIComponent(form.find("input[name='url']:first").val());
  132. // Get the embed code for this website.
  133. ADBULL.api.request("POST", argString, function (data, status) {
  134. $("#button-adbull-register-continue").removeClass("busy");
  135. ADBULL.showCode(data.data);
  136. });
  137. }
  138. });
  139. });
  140. $.facebox(panel);
  141. }
  142. };
  143. $(document).ready(ADBULL.load);