SammyKake

4chon Ext

May 17th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        4chon-ext
  3. // @namespace   4chon-ext
  4. // @description Hides post threads on 4chon (and possibly other Tinyboards). DO NOT USE
  5. // @include     http://4chon.net/*
  6. // @include     http://www.4chon.net/*
  7. // @exclude     http://4chon.net/mod.php?/*
  8. // @exclude     http://www.4chon.net/mod.php?/*
  9. // ==/UserScript==
  10. // ==Authors==
  11. //      Wriggle !!wvZ72pdJVA
  12. //      TheGent
  13. //==License==
  14. //  The "Do Whatever the Fuck You Want License"
  15. //      You are free to redistribute, edit, and otherwise mutilate this file to your hearts content
  16. //      In doing so, you agree to keep the original authors listed in this file as they are.
  17.  
  18. try {
  19.     var test = unsafeWindow;
  20. } catch (e) {
  21.     unsafeWindow = window;
  22. };
  23.  
  24. // Add jQuery
  25. (function () {
  26.     var head = document.getElementsByTagName('head')[0];
  27.     var script = document.createElement('script');
  28.     script.type = 'text/javascript';
  29.     script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js';
  30.     script.addEventListener('load', function () {
  31.         var script = document.createElement("script");
  32.         script.textContent = "(" + jQueryLoaded + ")();";
  33.         head.appendChild(script);
  34.     }, false);
  35.  
  36.     head.appendChild(script);
  37. })();
  38.  
  39. // All site code must be inside this function
  40. function jQueryLoaded() {
  41.     /*
  42.         Initializes the script
  43.         Checks for what sort of page the user is on, reacts accordingly
  44.     */
  45.     init = function () {
  46.         //Begin with checking for OP posts
  47.         var threadOPs = $('div.op');
  48.         if (threadOPs.length > 0) {
  49.             preparePage();
  50.         } else {
  51.             //Exit Script, they're not on a board index or in a thread.
  52.             return false;
  53.         };
  54.     };
  55.  
  56.     preparePage = function () {
  57.         //Get all Posts
  58.         var posts = $('div.post');
  59.         var isIndex = $('div.post:first').parentsUntil('form').filter('div:not(.post)')[0];
  60.         if (!isIndex) {
  61.             posts = posts.filter(':gt(0)');
  62.         };
  63.         //Append the [Hide] link on each post's info section
  64.         var hideLink = posts
  65.                         .children('p.intro')
  66.                         .append('<a class="hide" href="">[Hide]</a>')
  67.                             .children('a.hide');
  68.         //Bind the [Hide] link's click event
  69.         hideLink.bind('click', function () {
  70.             var post = $(this).parent().parent();
  71.             if (post.hasClass('op')) {
  72.                 var rawID = post
  73.                         .parent()
  74.                         .attr('id');
  75.                 var id = stripForID(rawID);
  76.                 hideThread(id);
  77.             } else {
  78.                 var rawID = post.attr('id');
  79.                 var id = stripForID(rawID);
  80.                 hideReply(id);
  81.             };
  82.             return false;
  83.         });
  84.         //Check to see if there's posts to hide, other database maintenance
  85.         var hideList = getHideList();
  86.         var now = new Date();
  87.         for (postID in hideList) {
  88.             var expDate = hideList[postID]['expDate'];
  89.             if (expDate > now.getTime()) {
  90.                 hidePost(postID);
  91.             } else {
  92.                 delFromHideList(postID);
  93.             };
  94.         };
  95.  
  96.         // Code for the Filter configuration box
  97.         var ConfigBox = "<div class='ConfigBox' style ='border: 1px solid; position: absolute; top: 100px; right:0px'>";
  98.  
  99.         // All boxes are hidden
  100.         ConfigBox += "<div id='tab0' class='config_section'>";
  101.         ConfigBox += "</div>";
  102.  
  103.         // The filter
  104.         ConfigBox += "<div id='tab1' class='config_section'>";
  105.         ConfigBox += "<table border='0'>";
  106.         ConfigBox += "<center><span class='heading'>Filter by Content</span>";
  107.         ConfigBox += "<em>Seperate values with a semi-colon,<br>regex not yet implemented.</em></center>";
  108.         ConfigBox += "<form><tr><td>Name:    </td><td><input class='filter' id='NameFilter' rows='1' cols='10'/></td></tr>";
  109.         ConfigBox += "<tr><td>Trip:    </td><td><input class='filter' id='TripFilter' rows='1' cols='10'/></td></tr>";
  110.         ConfigBox += "<tr><td>Email:   </td><td><input class='filter' id='EmailFilter' rows='1' cols='10'/></td></tr>";
  111.         ConfigBox += "<tr><td>Subject: </td><td><input class='filter' id='SubjectFilter' rows='1' cols='10'/></td></tr>";
  112.         ConfigBox += "<tr><td>Comment: </td><td><input class='filter' id='CommentFilter' rows='1' cols='10'/></td></tr>";
  113.         ConfigBox += "<tr><td>Forced Name:</td><td><input class='filter' id='ForcedName' rows='1' cols='10'/></td></tr></form>";
  114.         ConfigBox += "</table>";
  115.         ConfigBox += "<center><a class='filter' href=''>Apply Filter</a> (will reload).</center>";
  116.         ConfigBox += "</div>";
  117.  
  118.         // Another config tab
  119.         ConfigBox += "<div id='tab2' class='config_section'>";
  120.         ConfigBox += '<a class="flush" href=""><center>[Flush]</center></a> <br><i>(unhides all hidden posts and resets the filters)</i>';
  121.         ConfigBox += "</div>";
  122.  
  123.         // Another config tab
  124.         ConfigBox += "<div id='tab3' class='config_section'>";
  125.         ConfigBox += "<marquee>キタ━━<blink>(゚∀゚)</blink>━━ !!!!!</marquee>";
  126.         ConfigBox += "</div>";
  127.         ConfigBox += "</div>";
  128.  
  129.         // Links to the tabs themselves
  130.         ConfigBox += "<ul class='tabs' style='float: left;list-style: none; padding: 10px; position: absolute;top: 0px; right:0px'>";
  131.         ConfigBox += "<li><a href='#tab0'>Hide</a></li>";
  132.         ConfigBox += "<li><a href='#tab1'>Filter</a></li>";
  133.         ConfigBox += "<li><a href='#tab2'>Flush</a></li>";
  134.         ConfigBox += "<li><a href='#tab3'>Misc</a></li>";
  135.         ConfigBox += "</ul>";
  136.  
  137.  
  138.         //      $("ul.tabs.li").css("float", "left");
  139.         //      $('ul.tabs.li').css('list-style', 'none');
  140.         //      $('ul.tabs.li').css('position', 'relative');
  141.         //      $('ul.tabs.li').css('padding', '9px');
  142.         $('config_section').css('padding', '10px');
  143.         $('config_section').css('font-size', '1.2em');
  144.  
  145.  
  146.  
  147.         $('body')
  148.                 .prepend(ConfigBox)
  149.                 .children('div.ConfigBox')
  150.                 .find('a.filter')
  151.                 .bind('click',
  152.                 function () {
  153.                     // Get data
  154.                     var NameFilter = $("#NameFilter").val();
  155.                     var TripFilter = $("#TripFilter").val();
  156.                     var EmailFilter = $("#EmailFilter").val();
  157.                     var SubjectFilter = $("#SubjectFilter").val();
  158.                     var CommentFilter = $("#CommentFilter").val();
  159.                     var ForcedName = $("#ForcedName").val();
  160.  
  161.                     // Make it a JSON
  162.                     var FilterData = {
  163.                         'name': NameFilter,
  164.                         'trip': TripFilter,
  165.                         'subject': SubjectFilter,
  166.                         'email': EmailFilter,
  167.                         'comment': CommentFilter,
  168.                         'forcedname': ForcedName
  169.                     };
  170.  
  171.                     // store it
  172.                     localStorage.setItem("FilterData", JSON.stringify(FilterData));
  173.                 });
  174.  
  175.         //On Click Event
  176.         $("ul.tabs li").click(function () {
  177.  
  178.             $("ul.tabs li").removeClass("active"); //Remove any "active" class
  179.             $(this).addClass("active"); //Add "active" class to selected tab
  180.             $(".config_section").hide(); //Hide all tab content
  181.  
  182.             var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
  183.             $(activeTab).fadeIn(); //Fade in the active ID content
  184.             return false;
  185.         });
  186.  
  187.         //hide all initially
  188.         $(".config_section").hide(); //Hide all content
  189.         $("ul.tabs li:first").addClass("active").show(); //Activate first tab
  190.         $(".config_section:first").show(); //Show first tab content
  191.  
  192.         // bind to flush button
  193.         $('body').children('div.ConfigBox')
  194.                 .find('a.flush')
  195.                 .bind('click', function () {
  196.                     localStorage.removeItem('hiddenPosts');
  197.                     localStorage.removeItem('FilterData');
  198.                     $('.unhide').click();
  199.                     return false;
  200.                 });
  201.  
  202.         // Set the stored filters into the text boxes
  203.         var CompleteFilterList = $.parseJSON(localStorage.getItem("FilterData"));
  204.         if (CompleteFilterList) {
  205.             $("#NameFilter").val(CompleteFilterList.name);
  206.             $("#TripFilter").val(CompleteFilterList.trip);
  207.             $("#EmailFilter").val(CompleteFilterList.email);
  208.             $("#SubjectFilter").val(CompleteFilterList.subject);
  209.             $("#CommentFilter").val(CompleteFilterList.comment);
  210.             $("#ForcedName").val(CompleteFilterList.forcedname);
  211.         }
  212.  
  213.         // Disable Comment and Email fields because NOT_YET_IMPLEMENTED
  214.         $("#EmailFilter").attr('disabled', 'yes');
  215.         $("#CommentFilter").attr('disabled', 'yes');
  216.  
  217.         filterByContent();
  218.  
  219.     };
  220.    
  221.     isThread = function (id) {
  222.         var post = $('#thread_' + id);
  223.         if (post[0]) {
  224.             return true;
  225.         } else {
  226.             return false;
  227.         }
  228.     };
  229.  
  230.     stripForID = function (string) {
  231.         try { var ret = string.match(/(\d+)/)[1]; }
  232.         catch (e) { return false; }
  233.         //alert(ret);
  234.         return ret;
  235.     };
  236.  
  237.     hidePost = function (id) {
  238.         if (isThread(id)) {
  239.             hideThread(id);
  240.         } else {
  241.             hideReply(id);
  242.         };
  243.     };
  244.  
  245.     getHideList = function (boardID) {
  246.         var list = localStorage.getItem("hiddenPosts");
  247.         if (list) {
  248.             return $.parseJSON(list);
  249.         } else {
  250.             return {};
  251.         };
  252.     };
  253.  
  254.     addToHideList = function (postID, boardID) {
  255.         //Set up data
  256.         var postID = postID.toString(),
  257.                 hideList = getHideList(),
  258.                 expDate = new Date();
  259.         expDate = expDate.setDate(expDate.getDate() + 30).toString();
  260.         //Fill in Post data
  261.         if (!hideList[boardID]) {
  262.             hideList[boardID] = {};
  263.         };
  264.         hideList[boardID][postID] = {
  265.             'expDate': expDate
  266.         };
  267.         //Set item into localStorage
  268.         localStorage.setItem("hiddenPosts", JSON.stringify(hideList));
  269.     };
  270.  
  271.  
  272.     delFromHideList = function (postID, boardID) {
  273.         //Get List
  274.         hideList = getHideList();
  275.         //Remove entry
  276.         delete hideList[boardID][postID];
  277.         //Save List
  278.         localStorage.setItem("hiddenPosts", JSON.stringify(hideList));
  279.     };
  280.  
  281.  
  282.     hideThread = function (id) {
  283.         //Find Post (and picture, and posts) by id
  284.         var post = $('#thread_' + id);
  285.         //Check if it actually exists
  286.         if (post[0]) {
  287.             //Hide thread content
  288.             post
  289.                 .children(':not(hr)')
  290.                 .hide();
  291.             //Replace with "Thread Hidden [Unhide]"
  292.             post
  293.                 .prepend('<div class="placeholder"><p>Thread Hidden. <a class="unhide" href="">[Unhide]</a></p></div>')
  294.                     .find('a.unhide')
  295.                     .bind('click', function () {
  296.                         var rawID = $(this).parent().parent().parent().attr('id');
  297.                        
  298.                         var id = stripForID(rawID);
  299.                         showThread(id);
  300.                         return false;
  301.                     });
  302.             //Save change
  303.             addToHideList(id);
  304.         };
  305.     };
  306.  
  307.     showThread = function (id) {
  308.         //Find Hidden thread by id
  309.         var post = $('#thread_' + id);
  310.         //Check if it actually exists
  311.         if (post[0]) {
  312.             //Remove placeholder
  313.             post.children('.placeholder').remove();
  314.             //Show thread content
  315.             post.children().show();
  316.             //Save change
  317.             delFromHideList(id);
  318.         };
  319.     };
  320.  
  321.     hideReply = function (id) {
  322.         //Find Reply by id
  323.         var post = $('#reply_' + id);
  324.         //Check that it actually exists
  325.         if (post[0]) {
  326.             //Hide reply content
  327.             post.children().hide();
  328.             //Replace with "Post Hidden [Unhide]"
  329.             post
  330.                 .prepend('<div class="placeholder"><p>Post Hidden. <a class="unhide" href="">[Unhide]</a></p></div>')
  331.                     .find('a.unhide')
  332.                     .bind('click', function () {
  333.                         var rawID = $(this).parent().parent().parent().attr('id');
  334.                         var id = stripForID(rawID);
  335.                         showReply(id);
  336.                         return false;
  337.                     });
  338.             //Save change
  339.             addToHideList(id);
  340.         };
  341.     };
  342.  
  343.     showReply = function (id) {
  344.         //Find Hidden Reply
  345.         var post = $('#reply_' + id);
  346.         //Check that it actually exists
  347.         if (post[0]) {
  348.             //Remove placeholder
  349.             post.children('.placeholder').remove();
  350.             //Show reply content
  351.             post.children().show()
  352.             //Save change
  353.             delFromHideList(id);
  354.         };
  355.     };
  356.  
  357.     hideImageByPostID = function (id) {
  358.         //Find Post by id, get it's picture
  359.  
  360.         //Hide Picture
  361.  
  362.         //Replace with "Image Hidden [Unhide]"
  363.     };
  364.  
  365.     showImageByPostID = function (id) {
  366.         //Find Post by id, find it's picture
  367.  
  368.         //Remove placeholder
  369.  
  370.         //Show Picture
  371.     };
  372.  
  373.     filterByContent = function () {
  374.         var CompleteFilterList = $.parseJSON(localStorage.getItem("FilterData"));
  375.         var NameList, TripList, EmailList, SubjectList, CommentList, ForcedName;
  376.         if ((CompleteFilterList == 'null') || (CompleteFilterList == null)) return 0;
  377.         if (CompleteFilterList.name != "") NameList = CompleteFilterList.name.split(';');
  378.         if (CompleteFilterList.trip != "") TripList = CompleteFilterList.trip.split(';');
  379.         if (CompleteFilterList.email != "") EmailList = CompleteFilterList.email.split(';');
  380.         if (CompleteFilterList.subject != "") SubjectList = CompleteFilterList.subject.split(';');
  381.         if (CompleteFilterList.comment != "") CommentList = CompleteFilterList.comment.split(';');
  382.         if (CompleteFilterList.forcedname != "") ForcedName = CompleteFilterList.forcedname;
  383.  
  384.         if (CompleteFilterList.name != "") filterSubroutine('name', NameList);
  385.         if (CompleteFilterList.subject != "") filterSubroutine('subject', SubjectList);
  386.         if (CompleteFilterList.trip != "") filterSubroutine('trip', TripList);
  387.  
  388.         if (CompleteFilterList.forcedname != '') {
  389.             $('div.post').find('span.trip').each(function (index) { $(this).text("") });
  390.             $('div.post').find('span.name').each(function (index) { $(this).text(ForcedName) });
  391.         }
  392.  
  393.         // Find matching emails
  394.         if (CompleteFilterList.email != "") for (var i = 0; i < EmailList.length; i++) {
  395.         }
  396.  
  397.         // find matching comments
  398.         if (CompleteFilterList.comment != "") for (var i = 0; i < CommentList.length; i++) {
  399.             ; //alert(CommentList[i] + "\n" + CommentList.length);
  400.         }
  401.     }
  402.  
  403.     filterSubroutine = function (tag, TargetArray) {
  404.         for (var i = 0; i < TargetArray.length; i++) {
  405.             AllPost = $('div.post').find('span.' + tag);
  406.  
  407.             AllPost.each(function (index) {
  408.                 if ($(this).text() == TargetArray[i]) {
  409.                     if (!$(this).parent().parent().attr('id')) { // we are dealing with a post with an email field
  410.                         hidePost(stripForID($(this).parent().parent().parent().attr('id')));
  411.                     }
  412.                     else {
  413.                         hidePost(stripForID($(this).parent().parent().parent().attr('id')));
  414.                     }
  415.                 }
  416.             }); // close AllPost.each()
  417.         }
  418.     }
  419.  
  420.     init();
  421. };
Advertisement
Add Comment
Please, Sign In to add comment