A LILiK chrome extension for the well known 9gag website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. function setupVideoObject(){
  2. return {
  3. target: null,
  4. gifUrl: null,
  5. name: null
  6. };
  7. }
  8. function setLilikLogo(){
  9. console.log(chrome.extension.getURL("assets/logo100.png"));
  10. jQuery(" header#top-nav a.logo").css({
  11. "background-image": "url("+chrome.extension.getURL("assets/logo100.png")+")",
  12. "background-repeat": "no-repeat",
  13. "background-size": "30px 30px"
  14. });
  15. }
  16. //roll out long post in home page:
  17. function setLongPostListener(){
  18. jQuery("#list-view-2").on( "click", ".badge-evt.post-read-more", function( event ) {
  19. event.preventDefault();
  20. post = jQuery(event.target);
  21. // var image = post.parent().find("img");
  22. var sidebar = jQuery("#sidebar-longPost");
  23. sidebar.removeClass("closed");
  24. sidebar.html("<img>");
  25. var image = jQuery("#sidebar-longPost img");
  26. image.on('error', function(){
  27. image.attr("src", post.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b.jpg");
  28. });
  29. image.attr("src", post.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b_v1.jpg");
  30. // post.remove();
  31. });
  32. jQuery(document).on('scroll', function(){
  33. var sidebar = jQuery("#sidebar-longPost");
  34. if( !sidebar.hasClass('closed') ){
  35. sidebar.addClass('closed');
  36. }
  37. });
  38. }
  39. function setupLongPostSidebar(){
  40. jQuery(".badge-page.page .main-wrap ").after("<div id='sidebar-longPost' class='closed'></div>");
  41. var pageHeight = jQuery(window).height();
  42. jQuery("#sidebar-longPost").css({"height": pageHeight});
  43. }
  44. //clean wake up overlay
  45. function cleanWakeUp(){
  46. jQuery("#overlay-container").remove();
  47. }
  48. //set a listener to videos right click
  49. function setVideoListener(){
  50. jQuery("#list-view-2").on('contextmenu', "video", function(e) {
  51. currentVideo.target = jQuery(event.target) ;
  52. currentVideo.gifUrl = currentVideo.target.parent().data("image").replace("a.gif", ".gif");
  53. currentVideo.name = currentVideo.target.parents("article").find("h2").text().trim() + ".gif";
  54. });
  55. }
  56. //download url
  57. function downloadURI(uri, name){
  58. var link = document.createElement("a");
  59. link.download = name;
  60. link.href = uri;
  61. link.click();
  62. }
  63. //night mode
  64. function nightMode(){
  65. if( isNightTime() ){
  66. toggleNight("on");
  67. }
  68. }
  69. function enableSoftTransitions( element ){
  70. jQuery(element).addClass("softTransitions");
  71. }
  72. function toggleNight( command ){
  73. nightClass = "night";
  74. var container = jQuery('#container');
  75. if( command ){
  76. switch( command ){
  77. case 'on':
  78. container.addClass( nightClass );
  79. break;
  80. case 'off':
  81. container.removeClass( nightClass );
  82. break;
  83. }
  84. }else{
  85. if( container.hasClass( nightClass ) ){
  86. toggleNight("off");
  87. }else{
  88. toggleNight("on");
  89. }
  90. }
  91. }
  92. function isNightTime(){
  93. var nightHour = 19;
  94. var date = new Date();
  95. var hours = date.getHours();
  96. if( hours >= nightHour )
  97. return true;
  98. else
  99. return false;
  100. }
  101. //init everything
  102. jQuery(document).ready(function() {
  103. setLongPostListener();
  104. cleanWakeUp();
  105. currentVideo = setupVideoObject();
  106. setVideoListener();
  107. enableSoftTransitions(jQuery("#container"));
  108. setupLongPostSidebar();
  109. nightMode();
  110. setLilikLogo();
  111. console.log("9gag Mod Successfully Loaded!");
  112. // hash = window.location.hash.substring(1);
  113. // if (hash){
  114. // count = 0;
  115. // while (count < 15){
  116. // count++;
  117. // article = jQuery("article[data-entry-id='" + window.location.hash +"']");
  118. // if (article.length){
  119. // jQuery("html, body").animate({ scrollTop: jQuery("article[data-entry-id='" + hash +"']").offset().top}, 1000);
  120. // break;
  121. // }else{
  122. // jQuery("html, body").animate({ scrollTop: jQuery("div.loading").offset().top}, 1000);
  123. // }
  124. // }
  125. // }
  126. });
  127. chrome.extension.onMessage.addListener(
  128. function(request, sender, sendResponse) {
  129. switch (request.command){
  130. case "copyGifUrl":
  131. sendResponse({ url: currentVideo.gifUrl });
  132. break;
  133. case "downloadGif":
  134. downloadURI( currentVideo.gifUrl , currentVideo.name);
  135. break;
  136. }
  137. }
  138. );