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.

270 lines
6.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. function setupVideoObject(){
  2. return {
  3. target: null,
  4. gifUrl: null,
  5. name: null,
  6. permalink: null
  7. };
  8. }
  9. function setLilikLogo(){
  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 sidebar = jQuery("#sidebar-content-mod");
  22. sidebar.removeClass("closed");
  23. sidebar.html("<img>");
  24. var image = jQuery("#sidebar-content-mod img");
  25. jQuery.get( post.attr('href'), function(content) {
  26. image.attr("src", jQuery(content).find('.badge-item-img').attr("src"));
  27. });
  28. });
  29. jQuery(document).on('scroll', function(){
  30. var sidebar = jQuery("#sidebar-content-mod");
  31. if( !sidebar.hasClass('closed') ){
  32. sidebar.addClass('closed');
  33. }
  34. });
  35. }
  36. function setupSidebar(){
  37. if( !jQuery("#sidebar-content-mod").length ){
  38. jQuery("#container").after("<div id='sidebar-content-mod' class='closed'></div>");
  39. var menuHeight = jQuery("#top-nav").height();
  40. }
  41. var pageHeight = jQuery(window).height();
  42. jQuery("#sidebar-content-mod").css({"height": pageHeight, top: menuHeight});
  43. }
  44. //clean wake up overlay
  45. function cleanWakeUp(){
  46. // jQuery('head').append('<script type="text/javascript">GAG.Configs._configs.configs.idlePopupIdleTime = 9007199254740991; clearTimeout(GAG.PageController._idlePopupTimer); </script>');
  47. // 31556926000 = 1 year in ms.
  48. var code = ['GAG.Configs._configs.configs.idlePopupIdleTime = 31556926000; clearTimeout(GAG.PageController._idlePopupTimer);'].join('\n');
  49. var script = document.createElement('script');
  50. script.textContent = code;
  51. (document.head||document.documentElement).appendChild(script);
  52. script.parentNode.removeChild(script);
  53. }
  54. //set a listener to videos right click
  55. function setVideoListener(){
  56. jQuery(".main-wrap").on('contextmenu', "video", function(e) {
  57. currentVideo.target = jQuery(event.target);
  58. currentVideo.gifUrl = currentVideo.target.parent().data("image");
  59. currentVideo.name = currentVideo.target.parents("article").find("h2").text().trim() + ".gif";
  60. });
  61. jQuery(".main-wrap").on('contextmenu', "a", function(e) {
  62. currentVideo.target = jQuery(event.target);
  63. var previous = currentVideo.target.parents("article").prev();
  64. var id = currentVideo.target.parents("article").data("entry-id");
  65. if (previous.length > 0){
  66. id = previous.data("entry-id");
  67. }
  68. currentVideo.permalink = [location.protocol, '//', location.host, location.pathname].join('') + "?id=" + id;
  69. if (currentVideo.permalink == undefined){
  70. console.log("Warning: unable to find the permalink for this post")
  71. }
  72. });
  73. }
  74. function setOnNewNodeListener(){
  75. jQuery(".main-wrap").on("DOMNodeInserted", function() {
  76. NSFWListener();
  77. });
  78. }
  79. function setOnWindowResizeListener(){
  80. jQuery(window).on('resize', function(){
  81. setupSidebar();
  82. });
  83. }
  84. //download url
  85. function downloadURI(uri, name){
  86. var link = document.createElement("a");
  87. link.download = name;
  88. link.href = uri;
  89. link.click();
  90. }
  91. function enableSoftTransitions( element ){
  92. jQuery(element).addClass("softTransitions");
  93. }
  94. //night mode
  95. function nightMode(){
  96. if( hasComments())
  97. setInvertCommentImages();
  98. if( isNightTime() ){
  99. toggleNight("on");
  100. }
  101. }
  102. function isNightTime(){
  103. var nightHour = 19;
  104. var morningHour = 7;
  105. var date = new Date();
  106. var hours = date.getHours();
  107. if( hours >= nightHour || hours <= morningHour)
  108. return true;
  109. else
  110. return false;
  111. }
  112. function hasComments(){
  113. return jQuery("#gcomment-widget-jsid-comment-sys").length;
  114. }
  115. function toggleNight( command ){
  116. nightClass = "night";
  117. var container = jQuery('#container');
  118. if( hasComments())
  119. var commentPosts = frames['gcomment-widget-jsid-comment-sys'].document.getElementsByClassName("post-comment")[0];
  120. if( command ){
  121. switch( command ){
  122. case 'on':
  123. container.addClass( nightClass );
  124. if( hasComments())
  125. addClass( commentPosts, nightClass );
  126. break;
  127. case 'off':
  128. container.removeClass( nightClass );
  129. if( hasComments())
  130. removeClass( commentPosts, nightClass );
  131. break;
  132. }
  133. }else{
  134. if( container.hasClass( nightClass ) ){
  135. toggleNight("off");
  136. }else{
  137. toggleNight("on");
  138. }
  139. }
  140. }
  141. function setInvertCommentImages(){
  142. // jQuery( "img", frames['gcomment-widget-jsid-comment-sys'].document ).css({"-webkit-filter" : "invert(100%)"});
  143. var cssLink = document.createElement("link")
  144. cssLink.href = chrome.extension.getURL("style.css");
  145. cssLink.rel = "stylesheet";
  146. cssLink.type = "text/css";
  147. frames['gcomment-widget-jsid-comment-sys'].document.body.appendChild(cssLink);
  148. }
  149. function showNSFW(){
  150. jQuery(".badge-nsfw-entry-cover").each(function() {
  151. jQuery(this).addClass("deobfuscated");
  152. var imageSource = "http://img-9gag-fun.9cache.com/photo/" + jQuery(this).parents("article").data("entry-id") + "_460s.jpg";
  153. // TODO: isn't enough a string instead of a jquery object?
  154. jQuery(this).html( jQuery('<img/>', { src: imageSource } ));
  155. });
  156. }
  157. function NSFWListener(){
  158. if ( !jQuery("#jsid-upload-menu").not(".deobfuscated").is(":visible") && !updatingDom ){
  159. updatingDom = true;
  160. showNSFW();
  161. updatingDom = false;
  162. }
  163. }
  164. //init everything
  165. jQuery(document).ready(function() {
  166. updatingDom = false;
  167. showNSFW();
  168. setOnNewNodeListener();
  169. setOnWindowResizeListener();
  170. setLongPostListener();
  171. cleanWakeUp();
  172. // TODO: this object is not in the right place:
  173. currentVideo = setupVideoObject();
  174. setVideoListener();
  175. enableSoftTransitions(jQuery("#container"));
  176. setupSidebar();
  177. nightMode();
  178. setLilikLogo();
  179. console.log("9gag Mod Successfully Loaded!");
  180. });
  181. chrome.extension.onMessage.addListener(
  182. function(request, sender, sendResponse) {
  183. switch (request.command){
  184. case "copyGifUrl":
  185. sendResponse({ url: currentVideo.gifUrl });
  186. break;
  187. case "copyPostPermalink":
  188. sendResponse({ url: currentVideo.permalink });
  189. break;
  190. case "downloadGif":
  191. downloadURI( currentVideo.gifUrl , currentVideo.name);
  192. break;
  193. }
  194. }
  195. );
  196. // javascript utils to manipulate classes:
  197. // necessary to hack the comments css
  198. function hasClass(ele,cls) {
  199. return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
  200. }
  201. function addClass(ele,cls) {
  202. if (!this.hasClass(ele,cls)) ele.className += " "+cls;
  203. }
  204. function removeClass(ele,cls) {
  205. if (hasClass(ele,cls)) {
  206. var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
  207. ele.className=ele.className.replace(reg,' ');
  208. }
  209. }