Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. var newHead = '<style>\
  2. .sexyWindow {\
  3. height: 100%;\
  4. width: 100%;\
  5. overflow-y: scroll;\
  6. font-family: monospace;\
  7. }\
  8. \
  9. #windowSelector {\
  10. position: absolute;\
  11. width: 320px;\
  12. right:32px;\
  13. top: 4px;\
  14. border: 1px solid #000;\
  15. background: #fff7ee;\
  16. padding:4px;\
  17. }\
  18. \
  19. #chat {\
  20. position: absolute;\
  21. right: 32px;\
  22. bottom: 4px;\
  23. border: 1px solid #000;\
  24. padding: 4px;\
  25. }\
  26. </style>';
  27.  
  28. var newRoot = '\
  29. <div id="msgLog" class="mainContainer"></div>\
  30. <div id="windowSelector"><b>Windows</b><br></div>\
  31. <div id="chat"></div>';
  32.  
  33. // Ensure we have jquery
  34. var jQuery = null;
  35. function tryToLoad() {
  36. if(jQuery == null) {
  37. setTimeout(tryToLoad, 100);
  38. return;
  39. }
  40.  
  41. doit();
  42. }
  43. tryToLoad();
  44.  
  45. // Include jquery
  46. document.write('<script src="http://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>');
  47.  
  48. var ourClient;
  49. var server = 'bee1.shamchat.com';
  50. function heyItsMeTheIFrame(server, client) {
  51. ourClient = client;
  52. }
  53.  
  54. function doit() {
  55. // Ensure no conflicts
  56. jQuery.noConflict();
  57.  
  58. jQuery(document.documentElement).children().each(function() {
  59. jQuery(this).hide();
  60. });
  61. jQuery(document.documentElement).html(newRoot);
  62. jQuery('body').append(newHead);
  63.  
  64. var chat = jQuery('#chat');
  65. chat.append(jQuery('<input>').attr('type','input').attr('id', 'chatClientID'));
  66. chat.append(jQuery('<input>').attr('type','input').attr('id', 'chatClientMSG').on('keydown', function(e) {
  67. if (e.which == 13) {
  68. var toSend = jQuery('#chatClientID').val();
  69. var msg = jQuery('#chatClientMSG').val();
  70. jQuery('#chatClientMSG').val('');
  71.  
  72. if(msg != '') {
  73. sendMessage(toSend, msg);
  74. }
  75. }
  76. }));
  77. chat.append(jQuery('<input>').attr('type','submit').click(function() {
  78. var toSend = jQuery('#chatClientID').val();
  79. var msg = jQuery('#chatClientMSG').val();
  80. jQuery('#chatClientMSG').val('');
  81.  
  82. if(msg != '') {
  83. sendMessage(toSend, msg);
  84. }
  85. }));
  86.  
  87. // Load a client
  88. $(document.body).grab(new Element("iframe", {
  89. src: "http://" + server + "/iframe.html",
  90. width: 0,
  91. height: 0,
  92. frameBorder: 0,
  93. style: "display:none;"
  94. }));
  95.  
  96. var windows = {};
  97. function newWindow(name) {
  98. windows[name] = jQuery('<div class="sexyWindow">');
  99.  
  100. var clicker = jQuery('<a>').attr('href','#').click(function() {
  101. selectWindow(name);
  102. }).text(name);
  103.  
  104. jQuery('#windowSelector').append(clicker);
  105. jQuery('#windowSelector').append('<br>');
  106.  
  107. if(name != 'main') {
  108. listenToChat(name);
  109. }
  110. }
  111. newWindow('main');
  112. log('Click someone to watch their conversation.<br>');
  113.  
  114. function selectWindow(name) {
  115. if(!windows[name]) {
  116. newWindow(name);
  117. }
  118.  
  119. jQuery('#msgLog').empty();
  120. jQuery('#msgLog').append(windows[name]);
  121. }
  122. selectWindow('main');
  123.  
  124. function log(msg, window) {
  125. if(!window) window = 'main';
  126.  
  127. // Grab it
  128. var div = windows[window];
  129.  
  130. if(!div) {
  131. console.log('Failed to find window: '+window);
  132. console.log(msg);
  133. return;
  134. }
  135.  
  136. // Scroll detection
  137. var shouldScroll = false;
  138. if(div.scrollTop() + div.innerHeight() + 10 >= div.prop('scrollHeight')) {
  139. shouldScroll = true;
  140. }
  141.  
  142. // Add message
  143. div.append(msg);
  144.  
  145. // Scroll to the bottom:
  146. if(shouldScroll) {
  147. div.scrollTop(div.prop('scrollHeight'));
  148. }
  149. }
  150.  
  151. function sendMessage(client, msg) {
  152. // Ensure we have a client
  153. if(!ourClient) return;
  154.  
  155. // Pack ans send message
  156. offWithItsHeader(new ourClient.Request({
  157. url: getRequestURL(server, "/send"),
  158. data: {
  159. msg: msg,
  160. id: client
  161. }
  162. })).send()
  163. };
  164.  
  165. function sendDisconnect(client) {
  166. // Ensure we have a client
  167. if(!ourClient) return;
  168.  
  169. // Pack ans send message
  170. offWithItsHeader(new ourClient.Request({
  171. url: getRequestURL(server, "/disconnect"),
  172. data: {
  173. id: client
  174. }
  175. })).send()
  176. };
  177.  
  178. function makeid() {
  179. var text = "";
  180. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  181.  
  182. for( var i=0; i < 10; i++ )
  183. text += possible.charAt(Math.floor(Math.random() * possible.length));
  184.  
  185. return text;
  186. }
  187.  
  188. // Searches for a peer
  189. var neededPeers = {};
  190. function findPeer(clientID, callback) {
  191. var ourID = makeid();
  192. neededPeers[ourID] = {
  193. clientID: clientID,
  194. callback: callback
  195. }
  196. sendMessage(clientID, ourID);
  197. }
  198.  
  199. var listenToID = {};
  200. function listenTo(clientID) {
  201. if(clientID) {
  202. listenToID[clientID] = [clientID];
  203.  
  204. log('Listening to ', clientID);
  205. log(clickableID2(clientID), clientID);
  206. log('<br>', clientID);
  207. }
  208. }
  209.  
  210. function listenToChat(clientID) {
  211. if(clientID) {
  212. listenToID[clientID] = clientID;
  213.  
  214. // Locate their partner
  215. findPeer(clientID, function(np, theirID) {
  216. log('Listening to ', clientID);
  217. log(clickableID2(clientID), clientID);
  218. log('\'s partner: ', clientID)
  219. log(clickableID2(theirID), clientID);
  220. log('<br>', clientID);
  221. listenToID[theirID] = clientID;
  222. });
  223.  
  224. log('Click someone to send that person a message.,br>');
  225. log('Listening to ', clientID);
  226. log(clickableID2(clientID), clientID);
  227. log('<br>', clientID);
  228. }
  229. }
  230.  
  231. function clickableID(clientID) {
  232. return jQuery('<a>').attr('href','#').text(clientID).click(function() {
  233. selectWindow(clientID);
  234. });
  235. }
  236.  
  237. function clickableID2(clientID) {
  238. return jQuery('<a>').attr('href','#').text(clientID).click(function() {
  239. jQuery('#chatClientID').val(clientID);
  240. jQuery('#chatClientMSG').focus();
  241. });
  242. }
  243.  
  244. // Maps IDs to names
  245. characterMap = {};
  246.  
  247. var ourFaye = new Faye.Client('http://bee1.shamchat.com/faye');
  248. ourFaye.addExtension({
  249. incoming:function(message, callback){
  250. if(message.data){
  251. message.data.channel = message.channel
  252. };callback(message);
  253. }
  254. });
  255. ourFaye.disable("autodisconnect");
  256. ourFaye.subscribe('/*', function(g){
  257. var clientID = g.channel.substring(1);
  258.  
  259. // Autosend a message
  260. var replyMessage = 'kBye!';
  261. if(g.event == 'chat') {
  262. if(g.data != replyMessage) {
  263. //sendMessage(clientID, replyMessage);
  264. }
  265.  
  266. log(clickableID(clientID));
  267. log((characterMap[clientID] ? (' (' + characterMap[clientID] + ')') : '') + ': ' + g.data + '<br>');
  268.  
  269. if(listenToID[clientID]) {
  270. log(clickableID2(clientID), listenToID[clientID]);
  271. log((characterMap[clientID] ? (' (' + characterMap[clientID] + ')') : '') + ': ' + g.data + '<br>', listenToID[clientID]);
  272. }
  273.  
  274. if(neededPeers[g.data]) {
  275. var np = neededPeers[g.data];
  276. neededPeers[g.data] = null;
  277.  
  278. if (typeof np.callback === "function") {
  279. np.callback(np, clientID);
  280. } else {
  281. log(neededPeers[g.data].clientID + ' IS CONNECTED TO ' + clientID + '<br>');
  282. }
  283. }
  284. }
  285.  
  286. if(g.event == 'connect') {
  287. characterMap[clientID] = g.otherCharacter;
  288. }
  289.  
  290. if(g.event == 'disconnect') {
  291. if(listenToID[clientID]) {
  292. log(clientID + (characterMap[clientID] ? (' (' + characterMap[clientID] + ')') : '') + ' has disconnected! <br>', listenToID[clientID]);
  293. }
  294.  
  295. // Free memory
  296. delete characterMap[clientID];
  297. }
  298.  
  299. //sendDisconnect(clientID);
  300. }, function(e){});
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement