Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.   var VOTE_DELAY = 1500; // Wait 1.5 seconds before voting again
  4.   var VOTE_YES_FOR = ['Julian Assange', 'Nigel Farage', 'Donald Trump'];
  5.  
  6.  
  7.   function buildVoteBatch(slides,  candidatesToVoteFor) {  
  8.     return _.map(slides, function(slide) {
  9.       var titleHtml = slide.title.value;
  10.       var title = titleHtml.match(/">(.*?)</)[1].trim();    
  11.       var shouldVoteYes = _.some(candidatesToVoteFor, function(target) { return title.match(target) });
  12.       var answerOption =  slide.options[1 - shouldVoteYes | 0];  
  13.      
  14.       return { title: title, slideId: slide.id, answer: answerOption }
  15.     });
  16.   }
  17.  
  18.   function submitInteraction(interactionId, result, successCallback) {
  19.     var payload = JSON.stringify({ interactionId: interactionId, result: result });
  20.  
  21.     var xhr = new XMLHttpRequest();
  22.     xhr.open("POST", "http://interaction.apester.com/interaction-submission", true);
  23.     xhr.onload = function() {
  24.         if(xhr.status === 204) {
  25.           successCallback();
  26.         }
  27.     }
  28.     xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");  
  29.     xhr.send(payload);
  30.   }
  31.  
  32.   function gethInteractionWidgetData(interactionId) {
  33.     return JSON.parse($('script[data-payload-url="/api/interaction/' + interactionId + '"]').text()).payload;
  34.   }
  35.  
  36.   var interactionWidget = gethInteractionWidgetData(window.interactionId);
  37.   var slides = interactionWidget.data.slides;
  38.   var batch = buildVoteBatch(slides, VOTE_YES_FOR);
  39.  
  40.   var result = _.map(batch, function(vote) { return  { slideId: vote.slideId, result: vote.answer.id }; });
  41.   var resultFriendly = _.map(batch, function(vote) { return  '' + vote.title + ': ' + vote.answer.value; });
  42.  
  43.   console.log(resultFriendly);
  44.  
  45.   setInterval(function() {
  46.     submitInteraction(interactionWidget.interactionId, result, function() { console.log('voted'); });
  47.   }, VOTE_DELAY);
  48.  
  49. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement