sumofus_main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. if(document.location.href.indexOf("sumofus") != -1){
  14. console.log("sumofus_main.js");
  15. function wildcard_match(str, rule) {
  16. return new RegExp("^" + rule.split("*").join(".*") + "$").test(str);
  17. }
  18. var page = window.location.href;
  19. /*
  20. if( wildcard_match(document.getElementsByTagName("pre")[0].innerHTML,'{"follow_up_url":"*"}') ){
  21. console.log(JSON.parse(document.getElementsByTagName("pre")[0].innerHTML)["follow_up_url"]);
  22. }
  23. */
  24. var style = "div { top: 5%; margin-left: 10%; margin-right: 10%; background-color: white;}"
  25. var sheet = document.createElement('style');
  26. sheet.innerHTML = style;
  27. document.body.appendChild(sheet);
  28. if( wildcard_match(page,"*://*actions.sumofus.org/api/pages/*/actions") ){
  29. console.log("Submitted petition")
  30. //console.log("Redirecting...");
  31. var link = document.body.innerText;
  32. link = JSON.parse(link);
  33. //console.log(link);
  34. document.body.innerHTML = "<h1>You have submitted this petition.</h1>";
  35. }
  36. function send_data(data){
  37. url = document.forms[0].action
  38. var xhr = new XMLHttpRequest();
  39. var url_encoded = "";
  40. var url_encoded_pairs = [];
  41. var name;
  42. for(name in data){
  43. temp = encodeURIComponent(name) + '=' + encodeURIComponent(data[name])
  44. url_encoded_pairs.push(temp);
  45. }
  46. url_encoded = url_encoded_pairs.join('&').replace(/%20/g, '+');
  47. xhr.addEventListener('load', function(event){
  48. console.log("loaded");
  49. res = JSON.parse(event.target.response);
  50. url = res["follow_up_url"]
  51. document.location = url;
  52. });
  53. xhr.addEventListener('error', function(event){
  54. console.log("Error submitting form.");
  55. });
  56. xhr.open('POST', url);
  57. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  58. xhr.send(url_encoded);
  59. }
  60. if( wildcard_match(page,"*://*actions.sumofus.org/a/*") ){
  61. console.log("Fixing campaign page");
  62. // Make some changes to the page so it's readable.
  63. // because I noticed there were two of these for some reason
  64. document.getElementsByTagName("button")[1].remove();
  65. document.getElementsByTagName("span")[0].innerHTML = "";
  66. var divs = document.getElementsByTagName("div");
  67. document.getElementsByClassName("noscript-notice")[0].remove();
  68. // If they change the HTML of the page this might break...
  69. //document.getElementsByClassName("petition-bar__content")[0].children[1].children[1].innerHTML = "";
  70. for ( i=0; i<document.styleSheets.length; i++) {
  71. void(document.styleSheets.item(i).disabled=true);
  72. }
  73. // apply some basic CSS
  74. var style = "div {margin: 50px; text-align: left; color: black; font-size: 12pt;}";
  75. var sheet = document.createElement('style');
  76. sheet.innerHTML = style;
  77. document.body.appendChild(sheet);
  78. // Make the button not submit a post request
  79. document.getElementsByClassName("button action-form__submit-button")[0].type = "button"
  80. // Add a listener to submit the form
  81. document.getElementsByClassName("button action-form__submit-button")[0].addEventListener("click", function(){
  82. entries = document.getElementsByClassName("form__content sweet-placeholder__field");
  83. data = []
  84. for(i = 0; i < entries.length; i++){
  85. data.push(entries[i].value);
  86. }
  87. form_id = document.getElementsByName("form_id")[0].value
  88. res = {
  89. country: data[2],
  90. email: data[0],
  91. form_id: form_id,
  92. name: data[1],
  93. phone: data[4],
  94. postal: data[3]
  95. }
  96. send_data(res);
  97. });
  98. } else{
  99. console.log("Getting current campaigns");
  100. var xhr = new XMLHttpRequest();
  101. xhr.onreadystatechange = function() {
  102. if(this["readyState"] == 4 && this["status"] == 200){
  103. var campaigns = JSON.parse(this.responseText);
  104. //console.log(campaigns);
  105. var s = "";
  106. var number = "";
  107. //html_template += '<button onclick="">Show Pictures</button><br>';
  108. for(var i = 0; i < campaigns.length; i += 1){
  109. number = (i+1)+"/"+campaigns.length+":";
  110. s = campaigns[i]["title"]+"<br>";
  111. html_template += number.link(campaigns[i]["url"])+s;
  112. html_template += "<img src="+'"'+campaigns[i]["image"]+'"'+"><br>";
  113. }
  114. document.body.innerHTML = html_template;
  115. }
  116. };
  117. var html_template = "<h1>[Simple SumOfUs.org]</h1>";
  118. // URL to get campaigns
  119. var url = "http://actions.sumofus.org/api/pages/featured.json?language=en";
  120. document.head.replaceWith("");
  121. document.body.innerHTML = "";
  122. xhr.open("GET", url);
  123. xhr.send();
  124. }
  125. }