Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. /**
  2. * WhatsApp Chrome Extension
  3. */
  4.  
  5. (function() {
  6.  
  7. var interval = setInterval(function () {
  8. var selector = document.querySelector(".app-wrapper-main");
  9. if(selector) {
  10. clearInterval(interval);
  11.  
  12. setTimeout(startService, 2000);
  13. }
  14. }, 500);
  15.  
  16. function startService()
  17. {
  18. console.debug("[FASTEE] Service ready");
  19.  
  20. longPullCreationQueue();
  21.  
  22. var chats = document.querySelectorAll(".chatlist .chat"), i, flag = false;
  23. for(i = 0;i<chats.length;i++) {
  24. watchNode(chats[i], function(mutations) {
  25. var chat;
  26. mutations.forEach(function(mutation) {
  27. chat = mutation.target.parentNode.parentNode.parentNode.parentNode.parentNode;
  28. activeChat = chat.className.indexOf("active") > -1;
  29. if(!activeChat && mutation.target.className == 'icon-meta unread-count' && (mutation.type == 'childList' || (mutation.type == 'attributes' && mutation.attributeName == 'class'))) {
  30. flag = true;
  31. }
  32. });
  33.  
  34. if(flag) {
  35. flag = false;
  36. handleBackgroundNotification(chat);
  37. }
  38. });
  39. }
  40. }
  41.  
  42. function watchNode(target, callback)
  43. {
  44. // create an observer instance
  45. var observer = new MutationObserver(function(mutations) {
  46. callback(mutations, observer);
  47. });
  48.  
  49. // configuration of the observer:
  50. var config = { childList: true, characterData: true, subtree: true };
  51.  
  52. // pass in the target node, as well as the observer options
  53. observer.observe(target, config);
  54. }
  55.  
  56. function handleBackgroundNotification(chat)
  57. {
  58. var offsetTop = window.innerHeight-window.screen.availHeight;
  59. var x = chat.getBoundingClientRect().left + (chat.offsetWidth/2);
  60. var y = offsetTop + chat.getBoundingClientRect().top + (chat.offsetHeight/2)
  61. var chatId = chat.getAttribute("data-reactid");
  62.  
  63. triggerMouse(x, y).then(function() {
  64. console.debug("[FASTEE] Background notification handled")
  65.  
  66. setTimeout(function () {
  67. markHandledMessages().then(function(response) {
  68. watchNode(document.querySelector("#pane-side"), function(mutations, callback) {
  69. markHandledMessages(true);
  70. })
  71. });
  72. }, 500);
  73. });
  74. }
  75.  
  76. function markHandledMessages(active)
  77. {
  78. active = active||false;
  79. return new Promise(function(resolve, reject) {
  80. var messages = document.querySelectorAll(".message-list .msg"), i, flag = false, newMessages = [];
  81. for(i = 0;i<messages.length;i++) {
  82. if(!active && messages[i].className.indexOf('msg-unread') > -1) {
  83. flag = true;
  84. continue;
  85. }
  86. else if(active && !messages[i].getAttribute("fastee-marked") && messages[i].className.indexOf('msg-unread') == -1) {
  87. flag = true;
  88. }
  89.  
  90. if(flag) {
  91. newMessages.push(getMessage(messages[i]));
  92. }
  93.  
  94. messages[i].setAttribute("fastee-marked", true);
  95. }
  96.  
  97. console.debug("[FASTEE] All messages processed with FASTEE", newMessages)
  98.  
  99. resolve(newMessages)
  100. });
  101. }
  102.  
  103. function getMessage(messageNode)
  104. {
  105. var loop = false;
  106. if(messageNode.querySelector(".message-author .emojitext")) {
  107. owner = messageNode.querySelector(".message-author .emojitext").innerText;
  108. }
  109. else {
  110. var chatlist = document.querySelectorAll(".chatlist .msg"), i, loop = false;
  111. messageNode.setAttribute("fastee-target", true);
  112. for(i = chatlist.length;i>0;i--) {
  113. if(chatlist[i].getAttribute("fastee-target")) {
  114. loop = true;
  115. continue ;
  116. }
  117.  
  118. if(loop && chatlist[i].querySelector(".message-author .emojitext")) {
  119. owner = chatlist[i].querySelector(".message-author .emojitext");
  120. }
  121.  
  122. }
  123. messageNode.removeAttribute("fastee-target")
  124. }
  125.  
  126. return {
  127. id : messageNode.getAttribute("data-reactid"),
  128. owner : owner,
  129. text : messageNode.querySelector(".message-text .emojitext").innerHTML,
  130. };
  131. }
  132.  
  133. window.newGroup = function(name, members, owner, ownerUid, forumId, conversationId)
  134. {
  135. return new Promise(function(resolve, reject) {
  136. document.querySelector(".pane-list-controls .menu-item:last-child").click();
  137. setTimeout(function () {
  138. document.querySelector(".pane-list-controls .active .dropdown a:first-child").click();
  139. triggerKeyboard(name).then(function() {
  140. if(!document.querySelector(".drawer-container-new-group .drawer-controls button")) {
  141. reject();
  142. return ;
  143. }
  144. document.querySelector(".drawer-container-new-group .drawer-controls button").click();
  145. setTimeout(function () {
  146. // var i = 0, hold = false;
  147. // while(i < members.length) {
  148. // if(hold) continue;
  149. //
  150. // hold = true;
  151. // triggerKeyboard(members[i]).then(function() {
  152. // document.querySelector(".chatlist .contact:first-child").click();
  153. // i++;
  154. // hold = false;
  155. // });
  156. // }
  157. createMembers(name, members, owner, ownerUid, forumId, conversationId);
  158. }, 1000);
  159. });
  160. }, 100);
  161. });
  162. }
  163.  
  164. function updateChatCreated(owner, ownerUid, forumId, conversationId, chatId) {
  165. var xmlHttp = new XMLHttpRequest();
  166. xmlHttp.onreadystatechange = function() {
  167. if (xmlHttp.readyState == 4) {
  168. if(xmlHttp.status == 204) {
  169. // resolve();
  170. }
  171. // else
  172. // error(xmlHttp);
  173. }
  174. };
  175. xmlHttp.open("POST", server.site+"/"+ownerUid+"/WHATSAPP/chats", true); // true for asynchronous
  176. xmlHttp.setRequestHeader("Authorization","Basic "+window.btoa(owner+":?"));
  177. xmlHttp.setRequestHeader("Session-Id",user.SessionId);
  178. xmlHttp.setRequestHeader("Content-Type","application/json;charset=UTF-8");
  179. xmlHttp.send(JSON.stringify({
  180. forumId: forumId,
  181. conversationId: conversationId,
  182. chatId: chatId
  183. }));
  184. window.location.reload();
  185. }
  186.  
  187. var i = -1;
  188. function createMembers(name, members, owner, ownerUid, forumId, conversationId)
  189. {
  190. i++;
  191. if(i == members.length) {
  192. i = -1;
  193. document.querySelector(".drawer-new-group .drawer-controls button").click();
  194. setTimeout(function() {
  195. updateChatCreated(owner, ownerUid, forumId, conversationId, document.querySelector(".chatlist .chat:first-child").getAttribute("data-reactid"))
  196. }, 5000)
  197. return ;
  198. }
  199.  
  200. var member = members[i];
  201. document.querySelector(".new-group-search input").value = "";
  202. triggerKeyboard(member).then(function() {
  203. if(document.querySelector(".chatlist .contact:first-child")) {
  204. document.querySelector(".chatlist .contact:first-child").click();
  205. }
  206.  
  207. setTimeout(function() {
  208. createMembers(name, members, owner, ownerUid, forumId, conversationId);
  209. }, 100)
  210. });
  211. }
  212.  
  213. window.sendMessage = function(chatId, text)
  214. {
  215. document.querySelector(".chatlist .chat[data-reactid='"+chatId+"']").click();
  216. triggerKeyboard(text).then(function() {
  217. document.querySelector("footer .block-compose button.send-container").click();
  218. });
  219. }
  220.  
  221. var server = {
  222. local: "http://localhost/stagingfastee/d9b762932b90c3f8aa99aa47aaf57a08",
  223. site: "https://stagingsdk.fastee.co/appserv.svc",
  224. }
  225. var user = {
  226. uid: "d9b762932b90c3f8aa99aa47aaf57a08",
  227. email: "pub.whatsapp@fastee.co",
  228. password: "?",
  229. Authorization: "Basic cHViLndoYXRzYXBwQGZhc3RlZS5jbzo/",
  230. }
  231. setTimeout(function () {
  232. user.SessionId = generateUUID();
  233. }, 100);
  234.  
  235. function triggerKeyboard(str)
  236. {
  237. return new Promise(function(resolve, error) {
  238. var xmlHttp = new XMLHttpRequest();
  239. xmlHttp.onreadystatechange = function() {
  240. if (xmlHttp.readyState == 4) {
  241. if(xmlHttp.status == 204) {
  242. setTimeout(function () {
  243. resolve(xmlHttp);
  244. }, 500);
  245. }
  246. else
  247. error(xmlHttp);
  248. }
  249. };
  250. xmlHttp.open("POST", server.local+"/WHATSAPP/sendkeys", true); // true for asynchronous
  251. xmlHttp.setRequestHeader("Authorization",user.Authorization);
  252. xmlHttp.send(JSON.stringify({
  253. text: str,
  254. }));
  255. })
  256. }
  257.  
  258. function triggerMouse(x, y)
  259. {
  260. return new Promise(function(resolve, error) {
  261. var xmlHttp = new XMLHttpRequest();
  262. xmlHttp.onreadystatechange = function() {
  263. if (xmlHttp.readyState == 4) {
  264. if(xmlHttp.status == 204) {
  265. setTimeout(function () {
  266. resolve(xmlHttp);
  267. }, 500);
  268. }
  269. else
  270. error(xmlHttp);
  271. }
  272. };
  273. xmlHttp.open("POST", server.local+"/WHATSAPP/mouseclick".replace("{uid}", user.uid), true); // true for asynchronous
  274. xmlHttp.setRequestHeader("Authorization",user.Authorization);
  275. xmlHttp.setRequestHeader("Content-Type","application/json");
  276. xmlHttp.send(JSON.stringify({
  277. x: x,
  278. y: y,
  279. }));
  280. })
  281. }
  282.  
  283. function longPullCreationQueue()
  284. {
  285. var xmlHttp = new XMLHttpRequest();
  286. xmlHttp.onreadystatechange = function() {
  287. if (xmlHttp.readyState == 4) {
  288. if(xmlHttp.status == 200) {
  289. var response = JSON.parse(xmlHttp.responseText).chat;
  290. window.newGroup(response.topic, response.members, response.owner, response.ownerUid, response.forumId, response.conversationId).then(function() {
  291. longPullCreationQueue();
  292. });
  293. }
  294. else if(xmlHttp.status == 204) {
  295. longPullCreationQueue()
  296. }
  297. // else
  298. // error(xmlHttp);
  299. }
  300. };
  301. xmlHttp.open("GET", server.site+"/"+user.uid+"/WHATSAPP/chats", true); // true for asynchronous
  302. xmlHttp.setRequestHeader("Authorization",user.Authorization);
  303. xmlHttp.setRequestHeader("Session-Id",user.SessionId);
  304. xmlHttp.send();
  305. }
  306.  
  307. var history = [];
  308. function generateUUID(){
  309. var d = new Date().getTime();
  310. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  311. var r = (d + Math.random()*16)%16 | 0;
  312. d = Math.floor(d/16);
  313. return (c=='x' ? r : (r&0x3|0x8)).toString(16);
  314. });
  315. history.push(uuid)
  316. return uuid;
  317. };
  318.  
  319. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement