Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var e621SlideGetJSON = function(url, parameters, successHandler, errorHandler) {
  2.     var xhr = new XMLHttpRequest();
  3.     xhr.open('post', url, true);
  4.     xhr.responseType = 'json';
  5.     xhr.onload = function() {
  6.         var status = xhr.status;
  7.         if (status == 200) {
  8.             successHandler && successHandler(xhr.response);
  9.         } else {
  10.             errorHandler && errorHandler(status);
  11.         }
  12.     };
  13.     xhr.setRequestHeader('Accept', "text/javascript, text/html, application/xml, text/xml, */*");
  14.     xhr.setRequestHeader('Content-type', "application/x-www-form-urlencoded; charset=UTF-8");
  15.     xhr.setRequestHeader('X-Prototype-Version', "1.6.0.3");
  16.     xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest");
  17.     xhr.send(parameters);
  18. };
  19. var e621SlideFailJSON = function(status) {
  20.     alert(status);
  21. };
  22. var e621SlideImageBuffer = Array();
  23. var e621SlideCurrentPostId = -1;
  24. var e621SlideCurrentIndex = -1;
  25. var e621SlideLoadImageArray = function() {
  26.     var thumbs = document.getElementsByClassName('thumb');
  27.     for (var i = 0; i < thumbs.length; i++) {
  28.         var currentThumb = thumbs[i];
  29.         var postId = currentThumb.id.substring(1);
  30.         if (currentThumb.className.indexOf("blacklisted") >= 0) {
  31.             e621SlideDownVote(postId);
  32.             continue;
  33.         }
  34.         e621SlideLoadImageData(postId);
  35.     }
  36. };
  37. var e621SlideLoadImageData = function(postId) {
  38.     var placeholder = {
  39.         postId: postId,
  40.         image: document.createElement('IMG'),
  41.         link: document.createElement('A')
  42.     };
  43.     placeholder.link.appendChild(placeholder.image);
  44.     placeholder.link.target = '_blank';
  45.     placeholder.image.style.border = 'none';
  46.     e621SlideImageBuffer.push(placeholder);
  47.     var succ = function(response) {
  48.         if (response.file_ext === 'swf') {
  49.             placeholder.image.src = '/download-preview.png';
  50.         } else {
  51.             placeholder.image.src = response.sample_url;
  52.         }
  53.         placeholder.link.href = '/post/show/' + postId;
  54.     };
  55.     e621SlideGetJSON('/post/show.json', 'id=' + postId, succ, e621SlideFailJSON);
  56. };
  57. var e621SlideContent = document.createElement('DIV');
  58. var e621SlideBackground = document.createElement('DIV');
  59. var e621SlideCell = document.createElement('DIV');
  60. var e621SlideAdvance = function(count) {
  61.     e621SlideCurrentIndex += count;
  62.     if (e621SlideCurrentIndex < 0) e621SlideCurrentIndex = 0;
  63.     if (e621SlideCurrentIndex < e621SlideImageBuffer.length) {
  64.         while (e621SlideCell.children.length > 0) {
  65.             e621SlideCell.removeChild(e621SlideCell.children[0]);
  66.         }
  67.         var slide = e621SlideImageBuffer[e621SlideCurrentIndex];
  68.         e621SlideCurrentPostId = slide.postId;
  69.         window.scrollTo(0, document.getElementById('i' + e621SlideCurrentPostId).offsetTop);
  70.         e621SlideCell.appendChild(slide.link);
  71.     } else {
  72.         e621SlideCloseLightbox();
  73.     }
  74. };
  75. var e621SlideDrawLightbox = function() {
  76.     //TODO: Show a given image by post id in a lightbox with upvote, downvote and favorite buttons
  77.     e621SlideBackground.style.position = 'fixed';
  78.     e621SlideBackground.style.width = '100%';
  79.     e621SlideBackground.style.height = '100%';
  80.     e621SlideBackground.style.top = '0px';
  81.     e621SlideBackground.style.left = '0px';
  82.     e621SlideBackground.style.background = '#000000';
  83.     e621SlideBackground.style.opacity = 0.5;
  84.     e621SlideBackground.style.zIndex = 98;
  85.     e621SlideContent.style.position = 'fixed';
  86.     e621SlideContent.style.display = 'table';
  87.     e621SlideContent.style.top = '0px';
  88.     e621SlideContent.style.left = '0px';
  89.     e621SlideContent.style.width = '100%';
  90.     e621SlideContent.style.height = '100%';
  91.     e621SlideContent.style.zIndex = 99;
  92.     e621SlideCell.style.width = '100%';
  93.     e621SlideCell.style.height = '100%';
  94.     e621SlideCell.style.display = 'table-cell';
  95.     e621SlideCell.style.textAlign = 'center';
  96.     e621SlideCell.style.verticalAlign = 'middle';
  97.     e621SlideContent.appendChild(e621SlideCell);
  98.     document.body.appendChild(e621SlideBackground);
  99.     document.body.appendChild(e621SlideContent);
  100.     e621SlideAdvance(1);
  101.     document.body.onkeydown = e621SlideKeyboardEvent;
  102. };
  103. var e621SlideGetVoteJSON = function(postId, vote, successHandler, errorHandler) {
  104.     var url = '/post/vote.json';
  105.     var parameters = 'id=' + postId + '&score=' + vote;
  106.     e621SlideGetJSON(url, parameters, successHandler, errorHandler);
  107. };
  108. var e621ErrStrResponse = 'Unexpected response. Change: ';
  109. var e621SlideUpVote = function(postId) {
  110.     var succ = function(response) {
  111.         if (response.change > 0) {
  112.             // fresh upvote (1) or changed from downvote (2)
  113.             console.log('Upvoted https://e621.net/post/show/' + postId);
  114.         } else if (response.change == -1) {
  115.             // removed upvote. correct to upvote
  116.             e621SlideGetVoteJSON(postId, 1, succ, e621SlideFailJSON);
  117.         } else {
  118.             console.err(e621ErrStrResponse + response.change);
  119.         }
  120.     };
  121.     e621SlideGetVoteJSON(postId, 1, succ, e621SlideFailJSON);
  122. };
  123. var e621SlideDownVote = function(postId) {
  124.     var succ = function(response) {
  125.         if (response.change < 0) {
  126.             // fresh downvote (-1) or changed from upvote (-2)
  127.             console.log('Downvoted https://e621.net/post/show/' + postId);
  128.         } else if (response.change == 1) {
  129.             // removed downvote. correct to downvote
  130.             e621SlideGetVoteJSON(postId, -1, succ, e621SlideFailJSON);
  131.         } else {
  132.             console.err(e621ErrStrResponse + response.change);
  133.         }
  134.     };
  135.     e621SlideGetVoteJSON(postId, -1, succ, e621SlideFailJSON);
  136. };
  137. var e621SlideFavorite = function() {
  138.     e621SlideUpVote();
  139.     //TODO: Add post to favorites
  140. };
  141. var e621SlideCloseLightbox = function() {
  142.     document.body.removeChild(e621SlideContent);
  143.     document.body.removeChild(e621SlideBackground);
  144.     document.body.onkeydown = function(){};
  145. };
  146. var e621SlideKeyboardEvent = function(e) {
  147.     switch(e.keyCode) {
  148.         case 38: // up
  149.         case 73: // i
  150.         case 87: // w
  151.         case 187: // =
  152.             e621SlideUpVote(e621SlideCurrentPostId);
  153.             e621SlideAdvance(1);
  154.             break;
  155.         case 40: // down
  156.         case 75: // k
  157.         case 83: // s
  158.         case 189: // -
  159.             e621SlideDownVote(e621SlideCurrentPostId);
  160.             e621SlideAdvance(1);
  161.             break;
  162.         case 39: // right
  163.         case 76: // l
  164.         case 68: // d
  165.             e621SlideAdvance(1);
  166.             break;
  167.         case 37: // left
  168.         case 65: // a
  169.         case 74: // j
  170.             e621SlideAdvance(-1);
  171.             break;
  172.         //TODO: open post in new window on enter or spacebar
  173.         case 13: // enter
  174.         case 32: // spacebar
  175.             e621SlideImageBuffer[e621SlideCurrentIndex].link.click();
  176.             e621SlideAdvance(1);
  177.             break;
  178.         default: // any other key closes the lightbox
  179.             e621SlideCloseLightbox();
  180.     }
  181.     e.preventDefault();
  182. };
  183. e621SlideLoadImageArray();
  184. e621SlideDrawLightbox();
  185.  
  186. //=====================================================================================
  187. // BOOKMARKLET VERSION
  188. //=====================================================================================
  189. javascript:var e621SlideGetJSON=function(a,b,c,e){var d=new XMLHttpRequest;d.open("post",a,!0);d.responseType="json";d.onload=function(){var a=d.status;200==a?c&&c(d.response):e&&e(a)};d.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*");d.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");d.setRequestHeader("X-Prototype-Version","1.6.0.3");d.setRequestHeader("X-Requested-With","XMLHttpRequest");d.send(b)},e621SlideFailJSON=function(a){alert(a)},e621SlideImageBuffer=[],e621SlideCurrentPostId=-1,e621SlideCurrentIndex=-1,e621SlideLoadImageArray=function(){for(var a=document.getElementsByClassName("thumb"),b=0;b<a.length;b++){var c=a[b],e=c.id.substring(1);0<=c.className.indexOf("blacklisted")?e621SlideDownVote(e):e621SlideLoadImageData(e)}},e621SlideLoadImageData=function(a){var b={postId:a,image:document.createElement("IMG"),link:document.createElement("A")};b.link.appendChild(b.image);b.link.target="_blank";b.image.style.border="none";e621SlideImageBuffer.push(b);e621SlideGetJSON("/post/show.json","id="+a,function(c){b.image.src="swf"===c.file_ext?"/download-preview.png":c.sample_url;b.link.href="/post/show/"+a},e621SlideFailJSON)},e621SlideContent=document.createElement("DIV"),e621SlideBackground=document.createElement("DIV"),e621SlideCell=document.createElement("DIV"),e621SlideAdvance=function(a){e621SlideCurrentIndex+=a;0>e621SlideCurrentIndex&&(e621SlideCurrentIndex=0);if(e621SlideCurrentIndex<e621SlideImageBuffer.length){for(;0<e621SlideCell.children.length;)e621SlideCell.removeChild(e621SlideCell.children[0]);a=e621SlideImageBuffer[e621SlideCurrentIndex];e621SlideCurrentPostId=a.postId;window.scrollTo(0,document.getElementById("i"+e621SlideCurrentPostId).offsetTop);e621SlideCell.appendChild(a.link)}else e621SlideCloseLightbox()},e621SlideDrawLightbox=function(){e621SlideBackground.style.position="fixed";e621SlideBackground.style.width="100%";e621SlideBackground.style.height="100%";e621SlideBackground.style.top="0px";e621SlideBackground.style.left="0px";e621SlideBackground.style.background="#000000";e621SlideBackground.style.opacity=0.5;e621SlideBackground.style.zIndex=98;e621SlideContent.style.position="fixed";e621SlideContent.style.display="table";e621SlideContent.style.top="0px";e621SlideContent.style.left="0px";e621SlideContent.style.width="100%";e621SlideContent.style.height="100%";e621SlideContent.style.zIndex=99;e621SlideCell.style.width="100%";e621SlideCell.style.height="100%";e621SlideCell.style.display="table-cell";e621SlideCell.style.textAlign="center";e621SlideCell.style.verticalAlign="middle";e621SlideContent.appendChild(e621SlideCell);document.body.appendChild(e621SlideBackground);document.body.appendChild(e621SlideContent);e621SlideAdvance(1);document.body.onkeydown=e621SlideKeyboardEvent},e621SlideGetVoteJSON=function(a,b,c,e){e621SlideGetJSON("/post/vote.json","id="+a+"&score="+b,c,e)},e621ErrStrResponse="Unexpected response. Change: ",e621SlideUpVote=function(a){var b=function(c){0<c.change?console.log("Upvoted https://e621.net/post/show/"+a):-1==c.change?e621SlideGetVoteJSON(a,1,b,e621SlideFailJSON):console.err(e621ErrStrResponse+c.change)};e621SlideGetVoteJSON(a,1,b,e621SlideFailJSON)},e621SlideDownVote=function(a){var b=function(c){0>c.change?console.log("Downvoted https://e621.net/post/show/"+a):1==c.change?e621SlideGetVoteJSON(a,-1,b,e621SlideFailJSON):console.err(e621ErrStrResponse+c.change)};e621SlideGetVoteJSON(a,-1,b,e621SlideFailJSON)},e621SlideFavorite=function(){e621SlideUpVote()},e621SlideCloseLightbox=function(){document.body.removeChild(e621SlideContent);document.body.removeChild(e621SlideBackground);document.body.onkeydown=function(){}},e621SlideKeyboardEvent=function(a){switch(a.keyCode){case 38:case 73:case 87:case 187:e621SlideUpVote(e621SlideCurrentPostId);e621SlideAdvance(1);break;case 40:case 75:case 83:case 189:e621SlideDownVote(e621SlideCurrentPostId);e621SlideAdvance(1);break;case 39:case 76:case 68:e621SlideAdvance(1);break;case 37:case 65:case 74:e621SlideAdvance(-1);break;case 13:case 32:e621SlideImageBuffer[e621SlideCurrentIndex].link.click();e621SlideAdvance(1);break;default:e621SlideCloseLightbox()}a.preventDefault()};e621SlideLoadImageArray();e621SlideDrawLightbox();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement