// ==UserScript== // @name NeoGAF - Hide Forum Threads // @namespace ThreadFilter // @description Hides threads on NeoGAF.com // @require http://code.jquery.com/jquery-latest.min.js // @require http://code.jquery.com/ui/1.11.2/jquery-ui.min.js // @include http://*neogaf.com/forum/forumdisplay.php?f=* // @version 0.7 // @downloadURL none // ==/UserScript== var sThreadTitle; currentVisibleThreads = []; nShownCount = 0; nOriginalPageCount = GetNextPage(); nPageCount = nOriginalPageCount; nAdditionalThreadCount = 0; $(document).ready(function() { CheckCloudSync(); CheckExpiredThreads(); CreateStyles(); CreateFilter(); CreateControlPanel(); CreateEventHandlers(); SetOptions(); UpdateHideLinks(); UpdateThreads(); }); function CreateStyles() { $("body").append(""); if (localStorage.getItem("HideLinkStyle") != "HideShow") $("body").append(GetRemoveStyle()); } function CreateFilter() { $('.large-button:first').parent().append(" "); } function GetRemoveStyle() { var HideThreadStyle = 'margin-left: -11px; margin-top: -38px;'; var HideThreadStyle2 = 'padding-left:10px; margin-left:-45px; margin-top:-2px;'; RemoveStyle = ''; return RemoveStyle; } function UpdateHideLinks() { $("td[id*='td_threadstatusicon_']").each(function( index,value ) { AddHideLink(this);}); } function CreateEventHandlers() { $('#ThreadFilter').change(function() { UpdateThreads(); UpdateHideLinks();}); $('#OpenFilterCP').click(OpenFilterCP); $('#AddWordButton').click(AddToWordFilter); $('#AddIgnoredUserButton').click(AddToIgnoredUserList); $('input[name="HideLinkStyle"]').change(function () { localStorage.setItem('HideLinkStyle', this.value); RemoveHideLinks(); SaveLastUpdate();}); $('#SearchAdditional').change(function() { localStorage.setItem('SearchAdditional', this.checked); SaveLastUpdate();}); $('#CloudSync').change(function() { localStorage.setItem('CloudSync', this.checked); ShowCloudSyncOptions(this); }); $('#CloudSyncFrequency').change(function() { localStorage.setItem('CloudSyncFrequency', this.value); SaveLastUpdate(); }); $('#CloudSyncKey').change(function() {CloudKeyChanged(this.value);}); $('#GenerateCloudSyncKey').click(GenerateCloudSyncKey); $('#ThreadExpiration').change(function() { localStorage.setItem('ThreadExpiration', this.checked); if (localStorage.getItem('ExpirationDays') == null) {localStorage.setItem('ExpirationDays',30); document.getElementById("ExpirationDays").value = localStorage.getItem("ExpirationDays");} $('#ThreadExpirationOptions').toggle(); SaveLastUpdate(); }); $('#ExpirationDays').change(function() {localStorage.setItem('ExpirationDays', this.value);}); } function RemoveHideLinks() { $("a[id*='RemoveThread']").remove(); $("a[id*='RemoveUser']").remove(); $("#RemoveStyle").remove(); if (localStorage.getItem("HideLinkStyle") != "HideShow") $("body").append(GetRemoveStyle()); } function SetOptions() { document.getElementById("ThreadFilter").value = CheckThreadFilterValue(localStorage.getItem("ThreadFilter")); if (localStorage.getItem("CloudSyncKey")) document.getElementById("CloudSyncKey").value = localStorage.getItem("CloudSyncKey"); if (!localStorage.getItem("HideLinkStyle")) localStorage.setItem("HideLinkStyle","X"); if (localStorage.getItem("ExpirationDays")) document.getElementById("ExpirationDays").value = localStorage.getItem("ExpirationDays"); CheckSettingOption("SearchAdditional"); CheckSettingOption("CloudSync"); CheckSettingOption("HideLinkStyle"); CheckSettingOption("ThreadExpiration"); $('#ThreadExpirationOptions').css('display',$('#ThreadExpiration').prop('checked') ? 'block' : 'none'); } function UpdateThreads() { nShownCount = 0; $('#ThreadFilter').blur(); localStorage.setItem('ThreadFilter',document.getElementById("ThreadFilter").value); currentVisibleThreads = []; $("td[id*=td_threadstatusicon_]").each(function(index) { nThreadID = $(this).attr('id').replace('td_threadstatusicon_',''); sThreadTitle = $('#thread_title_' + nThreadID).text(); sUserID = $(this).siblings().find('a[href*="member.php?u="]')[0].href.replace("http://www.neogaf.com/forum/member.php?u=",""); if (CheckThreadHidden(nThreadID,sThreadTitle,sUserID)) $(this).parent().hide(); else { currentVisibleThreads.push(nThreadID); nShownCount++; $(this).parent().show(); } }); if (localStorage.getItem("SearchAdditional") == "true") { nPageCount = nOriginalPageCount; GetAdditionalThreads(); } } function GetAdditionalThreads() { if (nShownCount < 40 && nPageCount <= (parseInt(nOriginalPageCount) + 5)) { var jqxhr = $.get(window.location.href + "&order=desc&page=" + nPageCount, ProcessAdditionalThread) .done(function() { nPageCount ++; GetAdditionalThreads(); }); } } function ProcessAdditionalThread(data) { var lastThread = $("td[id*='td_threadstatusicon_']").last(); $(data).find("td[id*='td_threadstatusicon_']").each(function(index, value) { nThreadID = $(this).attr('id').replace('td_threadstatusicon_',''); sThreadTitle = $(data).find('#thread_title_' + nThreadID).text(); sUserID = $(this).siblings().find('a[href*="member.php?u="]')[0].href.replace("http://www.neogaf.com/forum/member.php?u=",""); nAdditionalThreadCount = nAdditionalThreadCount + 1; if (!CheckThreadHidden(nThreadID, sThreadTitle, sUserID) && nShownCount < 40 && $.inArray(nThreadID,currentVisibleThreads) == -1) { $(lastThread).parent().after($(this).parent().clone().wrap('
').parent().html());
    	    	AddHideLink($("td[id*=td_threadstatusicon_" + nThreadID + "]"));    	
            	nShownCount++;
            	}
           
    	});
}
function CheckThreadFilterValue(sFilterValue)
{
    if (sFilterValue != "Unignored" && sFilterValue != "Ignored" && sFilterValue != "All")
        sFilterValue = "Unignored";
    
    return sFilterValue;
}
function IgnoreItem(event) { 
    var nCurrentID = event.data.param1;
    var sList = event.data.param2;
    var addItem = {};
    addItem.ID = nCurrentID;
    addItem.AddDate = new Date();
    
    if (sList == "IgnoreList")
        addItem.Title = $('#thread_title_' + nCurrentID).text();
    else if (sList == "IgnoredUserList")
        addItem.Username = $('a[href="member.php?u=' + nCurrentID + '"]').attr("title");
    
    CurrentList = GetListFromLocalStorage(sList);
    containsObject(nCurrentID,CurrentList,addItem);
    
    //if (nFoundIndex == -1) 
//    	CurrentList.push(addItem); 
  //  else 
    //	CurrentList.splice(nFoundIndex,1); 
        
    localStorage.setItem(sList, JSON.stringify(CurrentList));
    
    if (event.data.param3)
    	event.data.param3();
 
    AddHideLink(event.data.param4);
    
    SaveLastUpdate();   
    
    CheckCloudUpdateNeeded();
    
    return 0;
}
function AddHideLink(currentThread)
{
	    IgnoreList = GetListFromLocalStorage('IgnoreList');
    	IgnoredUserList = GetListFromLocalStorage('IgnoredUserList');
    
  	   	nThreadID = $(currentThread).attr('id').replace('td_threadstatusicon_','');
    
        MemberLink = $(currentThread).siblings().find('a[href*="member.php?u="]')[0];
    	nUserID = MemberLink.href.replace("http://www.neogaf.com/forum/member.php?u=","");
    
    	var bThreadIgnored = (containsObject(nThreadID,IgnoreList) == -1) ? false : true;
    	var bUserIgnored = (containsObject(nUserID,IgnoredUserList) == -1) ? false : true;
	    var sThreadFilterVal = $('#ThreadFilter').val();
    
    	
    	if (sThreadFilterVal == 'Unignored')
        {
            ThreadIgnoreText = "Hide";
            UserIgnoreText = "Remove";
        }
        else 
        {
            ThreadIgnoreText = bThreadIgnored ? "Show" : "Hide";
            UserIgnoreText = bUserIgnored ? "Restore" : "Remove";
        }
            
    	if ($(currentThread).has("a[id*='RemoveThread" + nThreadID + "']").length == 0)
	    {
       		var RemoveUserLinks = $('a[id*=RemoveUser' + nUserID + ']').toArray();
    
    		sUserIDReference = (RemoveUserLinks.length > 0) ? nUserID + RemoveUserLinks.length : nUserID;
        	
            if (localStorage.getItem("HideLinkStyle") == "HideShow")
                $(MemberLink).after("
" + UserIgnoreText + " User");
            else
                $(MemberLink).before("" + UserIgnoreText + " User");
                        		
            $('#RemoveUser' + sUserIDReference).click({param1: nUserID, param2: 'IgnoredUserList', param3: UpdateThreads, param4: currentThread}, IgnoreItem); 
	        
    	    $('', {
	  	    id: "RemoveThread" + nThreadID,
            text: ThreadIgnoreText
			}).appendTo(currentThread);
        
            $('#RemoveThread' + nThreadID).click({param1: nThreadID, param2: 'IgnoreList', param3: UpdateThreads, param4: currentThread}, IgnoreItem); 
    	}
    	else
        {
            $('#RemoveThread' + nThreadID).text(ThreadIgnoreText);
            $('a[id*=RemoveUser' + nUserID).text(UserIgnoreText + " User");
        }
}
function CheckThreadHidden(nThreadID, sThreadTitle,sUserID)
{
    	var sThreadFilterVal = $('#ThreadFilter').val();
    	var bWordFilterApplies = false;
    	var bUserFilterApplies = false;
    
    	IgnoreList = GetListFromLocalStorage('IgnoreList');
    	nThreadIndex = containsObject(nThreadID,IgnoreList);
    	
    	var bThreadIgnored = (nThreadIndex == -1) ? false : true;
    
        if ((bThreadIgnored && sThreadFilterVal == 'Unignored'))
        {
            return true;
        }
        else
        {
            bWordFilterApplies = WordFilterApplies(sThreadTitle);
                 
            if (bWordFilterApplies && sThreadFilterVal == 'Unignored')
          	{
              	return true;
           	}
            else 
           	{
                bUserFilterApplies = containsObject(sUserID,GetListFromLocalStorage('IgnoredUserList')) != -1 ? true : false;   
                      
                if (bUserFilterApplies && sThreadFilterVal == 'Unignored')
             	{
                    return true;
             	}
                else if (!bThreadIgnored && !bWordFilterApplies && !bUserFilterApplies && sThreadFilterVal == 'Ignored')
             	{
                 	return true;
             	}
           	}
             	
        }
    
    return false;
}
function WordFilterApplies(sThreadTitle)
{
    CurrentArea = GetCurrentArea();
    
    var bFilterApplies = false;
	WordList = GetListFromLocalStorage('WordList');
            
    jQuery.each(WordList,function (index)
	{
        
        if (this.Area == null || this.Area == 'All' || this.Area == CurrentArea)
        {    
        if (this.Type == 'plaintext')
		{
			sFragments = this.Word.split('*');
	
			bMatchesPattern = true;
			
            jQuery.each(sFragments, function(index)
			{
				if (sThreadTitle.toLowerCase().indexOf(this.toLowerCase()) == -1)
				{
					bMatchesPattern = false;
				}
			});
	
			if (bMatchesPattern === true)
        	{
        		bFilterApplies = true;
				return 0;
        	}
		}
		else if (this.Type == 'regularexpression')
		{
			sRegExMatches = sThreadTitle.match(this.Word);
	
			if (sRegExMatches)
            	{
                	bFilterApplies = true;
                	return 0;
            	}
		}
            }
    });
	
    return bFilterApplies;
 }   
 
function GetCurrentArea()
{
    CurrentLocation = window.location.href;
    
    if (CurrentLocation.indexOf("f=3") != -1 || CurrentLocation.indexOf("f=20") != -1)
        return "Off-Topic";
    else if (CurrentLocation.indexOf("f=2") != -1 || CurrentLocation.indexOf("f=8") != -1)
    	return "Gaming";
    
}
function AddToWordFilter(event) {
    newWord = $('#AddWordText').val();
    var addWord = {};
    addWord.Word = newWord;
    addWord.Type = $('input[name*=AddWordType]:checked').val();
    addWord.Area = $('input[name*=AddWordArea]:checked').val();
    WordList = GetListFromLocalStorage('WordList');
    nWordIndex = containsObject(addWord.Word, WordList);
    if (nWordIndex == -1) {
        WordList.push(addWord);
    }
    localStorage.setItem('WordList', JSON.stringify(WordList));
    $('#AddWordText').val('');
    $('input[name="AddWordArea"][value="All"]').prop('checked', true);
    UpdateListing('WordListing', 'WordList');
    
    localStorage.setItem("LastUpdate",new Date());
    CheckCloudUpdateNeeded();
}
function AddToIgnoredUserList(event) {
    newIgnoredUserText = $('#AddIgnoredUserText').val();
    var addIgnoredUser = {};
    addIgnoredUser.Username = newIgnoredUser;
    IgnoredUserList = GetListFromLocalStorage('IgnoredUserList');
    nIgnoredUserIndex = containsObject(addIgnoredUser.Username, IgnoredUserList);
    if (nIgnoredUserIndex == -1) {
        IgnoredUserList.push(addIgnoredUser);
    }
    localStorage.setItem('IgnoredUserList', JSON.stringify(IgnoredUserList));
    $('#AddIgnoredUserText').val('');
    
    UpdateListing('IgnoredUserListing', 'IgnoredUserList');
    
    localStorage.setItem("LastUpdate",new Date());
    CheckCloudUpdateNeeded();
}
function CreateControlPanel()
{
      var sControlPanel = "
' + this.Word + '  | ' + this.Type + ' | ' + (this.Area != null ? this.Area : 'All') + ' |