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.

445 lines
12 KiB

9 years ago
9 years ago
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. permalink: null
  7. };
  8. }
  9. function getSettings( setting ){
  10. var value = chrome.storage.sync.get( setting, function(obj){
  11. settings = obj;
  12. initExtension();
  13. });
  14. }
  15. function setLilikLogo(){
  16. jQuery(" header#top-nav a.logo").css({
  17. "background-image": "url("+chrome.extension.getURL("assets/logo100.png")+")",
  18. "background-repeat": "no-repeat",
  19. "background-size": "30px 30px"
  20. });
  21. }
  22. //roll out long post in home page:
  23. function setLongPostListener(){
  24. if( settings.long_post_visualization == "Sidebar" && settings.long_post_visualization_enabler){
  25. jQuery("#list-view-2").on( "click", ".badge-evt.post-read-more", function( event ) {
  26. event.preventDefault();
  27. post = jQuery(event.target);
  28. var sidebar = jQuery("#sidebar-content-mod");
  29. sidebar.removeClass("closed");
  30. sidebar.html("<img>");
  31. var image = jQuery("#sidebar-content-mod img");
  32. jQuery.get( post.attr('href'), function(content) {
  33. image.attr("src", jQuery(content).find('.badge-item-img').attr("src"));
  34. });
  35. });
  36. jQuery(document).on('scroll', function(){
  37. closeSidebar();
  38. });
  39. }
  40. if( settings.long_post_visualization == "Inline" && settings.long_post_visualization_enabler){
  41. jQuery("#list-view-2").on( "click", ".badge-evt.post-read-more", function( event ) {
  42. event.preventDefault();
  43. post = jQuery(event.target);
  44. var image = post.parents('article').find("img");
  45. jQuery.get( post.attr('href'), function(content) {
  46. image.attr("src", jQuery(content).find('.badge-item-img').attr("src"));
  47. });
  48. $(window).bind('scroll', post.parents('article').first(), function(event){
  49. console.log(event);
  50. console.log(event.data.offset());
  51. if($(window).scrollTop() > (event.data.offset().top + event.data.height())) {
  52. console.log('out');
  53. $(window).unbind('scroll');
  54. }
  55. });
  56. });
  57. jQuery(document).on('scroll', function(){
  58. closeSidebar();
  59. });
  60. }
  61. }
  62. function closeSidebar(){
  63. var sidebar = jQuery("#sidebar-content-mod");
  64. if( !sidebar.hasClass('closed') ){
  65. sidebar.addClass('closed');
  66. }
  67. }
  68. function setupSidebar(){
  69. if( !jQuery("#sidebar-content-mod").length ){
  70. jQuery("#container").after("<div id='sidebar-content-mod' class='closed'></div>");
  71. var menuHeight = jQuery("#top-nav").height();
  72. $('html').keydown(function(e){
  73. var sidebar = jQuery("#sidebar-content-mod");
  74. if (sidebar.is(':visible')){
  75. switch (e.keyCode){
  76. case 38:
  77. sidebar.scrollTop(sidebar.scrollTop()-20);
  78. e.preventDefault();
  79. break;
  80. case 40:
  81. sidebar.scrollTop(sidebar.scrollTop()+20);
  82. e.preventDefault();
  83. break;
  84. }
  85. var img = sidebar.find('img');
  86. console.log(img.height(), -img.offset().top + parseInt(sidebar.css('top'),10));
  87. if(img.height() <= -img.offset().top + parseInt(sidebar.css('top'),10)){
  88. closeSidebar();
  89. }
  90. }
  91. });
  92. }
  93. var pageHeight = jQuery(window).height();
  94. jQuery("#sidebar-content-mod").css({"height": pageHeight, top: menuHeight});
  95. }
  96. //clean wake up overlay
  97. function cleanWakeUp(){
  98. if (settings.disable_wakeup_enabler) {
  99. executeScriptOnPage('GAG.Configs._configs.configs.idlePopupIdleTime = 2147483647; clearTimeout(GAG.PageController._idlePopupTimer);');
  100. }
  101. }
  102. //set a listener to videos right click
  103. function setVideoListener(){
  104. jQuery(".main-wrap").on('contextmenu', "video", function(e) {
  105. currentVideo.target = jQuery(event.target);
  106. currentVideo.gifUrl = currentVideo.target.parent().data("image");
  107. currentVideo.name = currentVideo.target.parents("article").find("h2").text().trim() + ".gif";
  108. });
  109. jQuery(".main-wrap").on('contextmenu', "a", function(e) {
  110. currentVideo.target = jQuery(event.target);
  111. var previous = currentVideo.target.parents("article").prev();
  112. var id = currentVideo.target.parents("article").data("entry-id");
  113. if (previous.length > 0){
  114. id = previous.data("entry-id");
  115. }
  116. currentVideo.permalink = [location.protocol, '//', location.host, location.pathname].join('') + "?id=" + id;
  117. if (currentVideo.permalink == undefined){
  118. console.log("Warning: unable to find the permalink for this post")
  119. }
  120. });
  121. }
  122. function setOnNewNodeListener(){
  123. jQuery(".main-wrap").on("DOMNodeInserted", function(e) {
  124. if ( !updatingDom ){
  125. updatingDom = true;
  126. var element = jQuery(e.target);
  127. showNSFWPost(element);
  128. showVideoControls(element);
  129. updatingDom = false;
  130. }
  131. });
  132. }
  133. function setOnWindowResizeListener(){
  134. jQuery(window).on('resize', function(){
  135. setupSidebar();
  136. });
  137. }
  138. //download url
  139. function downloadURI(uri, name){
  140. var link = document.createElement("a");
  141. link.download = name;
  142. link.href = uri;
  143. link.click();
  144. }
  145. function enableSoftTransitions( element ){
  146. jQuery(element).addClass("softTransitions");
  147. }
  148. //night mode
  149. function nightMode(){
  150. if( hasComments())
  151. setInvertCommentImages();
  152. if( isNightTime() && settings.night_mode_enabler ){
  153. toggleNight("on");
  154. }
  155. }
  156. function isNightTime(){
  157. var nightHour = 19;
  158. var morningHour = 7;
  159. if( typeof settings.night_mode_starting_hour != "undefined" ){
  160. nightHour = settings.night_mode_starting_hour;
  161. }
  162. if( typeof settings.night_mode_ending_hour != "undefined"){
  163. morningHour = settings.night_mode_ending_hour;
  164. }
  165. // if( getSetting("night_mode_starting_hour") ){
  166. // nightHour = getSetting("night_mode_starting_hour");
  167. // console.log(nightHour);
  168. // }
  169. var date = new Date();
  170. var hours = date.getHours();
  171. if( hours >= nightHour || hours <= morningHour)
  172. return true;
  173. else
  174. return false;
  175. }
  176. function hasComments(){
  177. return jQuery("#gcomment-widget-jsid-comment-sys").length;
  178. }
  179. function toggleNight( command ){
  180. nightClass = "night";
  181. var container = jQuery('#container');
  182. var sectionHeader = jQuery('section.section-header');
  183. if( hasComments())
  184. var commentPosts = frames['gcomment-widget-jsid-comment-sys'].document.getElementsByClassName("post-comment")[0];
  185. if( command ){
  186. switch( command ){
  187. case 'on':
  188. container.addClass( nightClass );
  189. sectionHeader.addClass( nightClass );
  190. if( hasComments())
  191. addClass( commentPosts, nightClass );
  192. break;
  193. case 'off':
  194. container.removeClass( nightClass );
  195. sectionHeader.removeClass( nightClass );
  196. if( hasComments())
  197. removeClass( commentPosts, nightClass );
  198. break;
  199. }
  200. }else{
  201. if( container.hasClass( nightClass ) ){
  202. toggleNight("off");
  203. }else{
  204. toggleNight("on");
  205. }
  206. }
  207. }
  208. function setInvertCommentImages(){
  209. // jQuery( "img", frames['gcomment-widget-jsid-comment-sys'].document ).css({"-webkit-filter" : "invert(100%)"});
  210. var cssLink = document.createElement("link")
  211. cssLink.href = chrome.extension.getURL("style.css");
  212. cssLink.rel = "stylesheet";
  213. cssLink.type = "text/css";
  214. frames['gcomment-widget-jsid-comment-sys'].document.body.appendChild(cssLink);
  215. }
  216. function showNSFWPost(e){
  217. if (e == undefined ){
  218. jQuery(".badge-nsfw-entry-cover").each(function() {
  219. showNSFWPost(jQuery(this));
  220. });
  221. }else{
  222. e.find(".badge-nsfw-entry-cover").addBack(".badge-nsfw-entry-cover").each(function() {
  223. var tmp = jQuery(this);
  224. tmp.addClass("lilik-deobfuscated");
  225. var imageSource = "http://img-9gag-fun.9cache.com/photo/" + tmp.parents("article").data("entry-id") + "_460s.jpg";
  226. // TODO: isn't enough a string instead of a jquery object?
  227. tmp.html( jQuery('<img/>', { src: imageSource } ));
  228. //TODO: show NSFW gif
  229. // tmp.find('img').hide();
  230. // var video = jQuery('<video preload="auto" poster="http://img-9gag-fun.9cache.com/photo/' + tmp.parents("article").data("entry-id") + '_460s.jpg" loop/>');
  231. // if (!tmp.hasClass("badge-evt")){
  232. // video.width("600px");
  233. // }else{
  234. // video.width("500px");
  235. // }
  236. // tmp.addClass("badge-animated-cover badge-track badge-track-no-follow");
  237. // divvo = jQuery('<div/>').addClass("badge-animated-container-animated");
  238. // divvo.append(video);
  239. // divvo.append('<div class="badge-animated-container-static gif-post presenting">\
  240. // <img class="badge-item-img" src="http://img-9gag-fun.9cache.com/photo/aw7VWyQ_460s.jpg" alt="No diving zone" style="visibility: hidden;">\
  241. // <span class="play badge-gif-play badge-auto-clicked">GIF</span>\
  242. // </div>');
  243. // tmp.append(divvo);
  244. // video.append('<source/>').error(function() {
  245. // // video not found, restore img
  246. // video.parents('div.badge-animated-container-animated').remove();
  247. // tmp.find('img').show();
  248. // tmp.removeClass("badge-animated-cover badge-track badge-track-no-follow");
  249. // if (!tmp.hasClass("badge-evt")){
  250. // tmp.click(function(e){e.preventDefault();});
  251. // tmp.addClass("badge-post-zoom zoomable").removeClass("badge-nsfw-entry-cover");
  252. // }
  253. // }).attr('src', 'http://img-9gag-fun.9cache.com/photo/' + tmp.parents("article").data("entry-id") + '_460sv.mp4', 'type', 'video/mp4');
  254. // video.click(function(){
  255. // if (this.paused == false) {
  256. // this.pause();
  257. // } else {
  258. // this.play();
  259. // }
  260. // });
  261. // divvo.click(function(e){
  262. // e.stopPropagation();
  263. // e.preventDefault();}
  264. // );
  265. });
  266. }
  267. }
  268. function showNSFW(e){
  269. if ( !jQuery("#jsid-upload-menu").not(".lilik-deobfuscated").is(":visible") && settings.nsfw_enabler){
  270. showNSFWPost(e);
  271. }
  272. }
  273. function showVideoControls(e){
  274. if ( settings.video_controls_enabler ){
  275. showVideoControlsPost(e);
  276. }
  277. }
  278. function showVideoControlsPost(e){
  279. if (e == undefined ){
  280. jQuery("video").each(function() {
  281. showVideoControlsPost(jQuery(this));
  282. });
  283. }else{
  284. e.each(function() {
  285. jQuery(this).find("video").addBack("video").on('play', function(event) {
  286. setTimeout(function(element){jQuery(element).find("video").addBack("video").attr('controls',true);}, 2000, event.target);
  287. });
  288. });
  289. }
  290. }
  291. function toogleZoom(){
  292. // TODO: comment and rewrite ammodo
  293. if (settings.toogle_zoom_enabler) {
  294. $('html').keydown(function(e){
  295. switch (e.keyCode){
  296. case 74:
  297. case 75:
  298. case 90:
  299. if (jQuery("#jsid-modal-post-zoom").is(':visible')){
  300. executeScriptOnPage('jQuery("a.badge-overlay-close.close-button").click();');
  301. }else if (e.keyCode == 90){
  302. var a = jQuery('article.badge-in-view.badge-in-view-focus').first().find('a').first();
  303. //a.click(function(e){e.preventDefault();});
  304. a.addClass("badge-post-zoom zoomable");
  305. executeScriptOnPage('jQuery("article.badge-in-view.badge-in-view-focus").first().find("a").first().click()');
  306. a.removeClass("badge-post-zoom zoomable");
  307. }
  308. break;
  309. }
  310. });
  311. }
  312. }
  313. //init everything
  314. jQuery(document).ready(function() {
  315. updatingDom = false;
  316. getSettings();
  317. });
  318. function initExtension(){
  319. showNSFW();
  320. showVideoControls()
  321. setOnNewNodeListener();
  322. setOnWindowResizeListener();
  323. setLongPostListener();
  324. toogleZoom();
  325. cleanWakeUp();
  326. // TODO: this object is not in the right place:
  327. currentVideo = setupVideoObject();
  328. setVideoListener();
  329. enableSoftTransitions(jQuery("#container"));
  330. setupSidebar();
  331. nightMode();
  332. setLilikLogo();
  333. console.log("9gag Mod Successfully Loaded!");
  334. }
  335. chrome.extension.onMessage.addListener(
  336. function(request, sender, sendResponse) {
  337. switch (request.command){
  338. case "copyGifUrl":
  339. sendResponse({ url: currentVideo.gifUrl });
  340. break;
  341. case "copyPostPermalink":
  342. sendResponse({ url: currentVideo.permalink });
  343. break;
  344. case "downloadGif":
  345. downloadURI( currentVideo.gifUrl , currentVideo.name);
  346. break;
  347. }
  348. }
  349. );
  350. // javascript utils to manipulate classes:
  351. // necessary to hack the comments css
  352. function hasClass(ele,cls) {
  353. return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
  354. }
  355. function addClass(ele,cls) {
  356. if (!this.hasClass(ele,cls)) ele.className += " "+cls;
  357. }
  358. function removeClass(ele,cls) {
  359. if (hasClass(ele,cls)) {
  360. var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
  361. ele.className=ele.className.replace(reg,' ');
  362. }
  363. }
  364. function executeScriptOnPage(code){
  365. var s = document.createElement('script');
  366. s.type = "text/javascript";
  367. s.textContent = code;
  368. (document.head||document.documentElement).appendChild(s);
  369. }