FriskyLingo / Slickdeals - Add Hide/Show Functionality

// ==UserScript==
// @name         Slickdeals - Add Hide/Show Functionality
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @updateURL    https://openuserjs.org/meta/FriskyLingo/Slickdeals_-_Add_HideShow_Functionality.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/FriskyLingo/Slickdeals_-_Add_HideShow_Functionality.user.js
// @match        http://slickdeals.net/
// @match        http://slickdeals.net/newsearch.php?*
// @match        https://slickdeals.net/
// @match        https://slickdeals.net/newsearch.php?*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_listValues
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js
// @require      https://raw.githubusercontent.com/andrewplummer/Sugar/2.0.4/dist/sugar.min.js
// ==/UserScript==
/* jshint -W097 */
'use strict';

$(document).ready(function() {
	Sugar.extend();
	
	var hiddenItems = [];
	var pageType = 0;
	
	$.getJSON('https://www.friskylingo.com/data/SlickDealsHiddenItems.json', function(data) {
		hiddenItems = data;
	});
		
	//Remove any items from the page that have been hidden by me
	$.each(hiddenItems, function(index, item) {
		$('div[data-threadid="' + item + '"], div[data-thread-id="' + item + '"]').remove();
	});
	
	if (window.location.href.indexOf('newsearch') < 0) {
		//Front Page
		pageType = 1;
		handleFrontPage(hiddenItems);
	} else {
		//Search Results Page
		pageType = 2;
		handleSearchResultsPage(hiddenItems);
	}
	
	//Handle "hide me" clicks
	$(document).on('click', 'a.hide-me', function(e) {
		e.preventDefault();
		
		//Get the element to remove
		var $elementToRemove, theThreadId;
		
		if (pageType == 1) {
			//Front Page
			$elementToRemove = $(this).closest('div.fpGridBox');
			theThreadId = $elementToRemove.data('threadid');
		} else if (pageType == 2) {
			//Search Results Page
			$elementToRemove = $(this).parent('div.resultRow');
			theThreadId = $elementToRemove.data('thread-id');
		}

		//Remove the element from the page
		$elementToRemove.remove();

		//Add the thread ID to the list of hidden items
		hiddenItems.push(theThreadId);
		
		//Remove duplicate items
		var uniqueItems = Sugar.Array.unique(hiddenItems);
		
		//Save the data to the server
		setJsonFileData(JSON.stringify(uniqueItems), 'data/SlickDealsHiddenItems.json');
	});
});

function handleFrontPage(hiddenItems) {
	$('div.fpGridBox.grid.giveaway, div.sideGridBox.au').hide();
	
	var $frontPageItems = $('div.fpItem');

	var $hideButton = $('<a href="#" class="hide-me icon icon-cancel-circle2"></a>');

	$frontPageItems.append($hideButton);

	$frontPageItems.each(function() {
		var $theFrontPageItem = $(this);
		var $theHideMe = $theFrontPageItem.find('a.hide-me');

		$theHideMe.position({
			my: 'right top',
			at: 'right top',
			of: $theFrontPageItem,
			collision: 'none'
		});
	});
}

function handleSearchResultsPage(hiddenItems) {
	$('div.ab, div.dealAlertRow').hide();
	
	var $searchResultItems = $('div.resultRow');

	var $hideButton = $('<a href="#" class="hide-me icon icon-cancel-circle2"></a>');

	$searchResultItems.append($hideButton);

	$searchResultItems.each(function() {
		var $theSearchResultItem = $(this);
		var $theHideMe = $theSearchResultItem.find('a.hide-me');

		$theHideMe.position({
			my: 'left top',
			at: 'left top',
			of: $theSearchResultItem,
			collision: 'none'
		});
	});
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Set JSON file data
//////////////////////////////////////////////////////////////////////////////////////////////
function setJsonFileData(theData, theFilePath) {
	var theDataObj = {
		data: theData,
		filePath: theFilePath
	};
	
	console.log(theDataObj);

	///*
	//console.log('posting to PHP file');
	$.ajax({
		url: 'https://www.friskylingo.com/SetTextFileContents.php',
		data: theDataObj,
		type: 'POST',
		crossDomain: true,
		success: function() {
			console.log('SUCCESS!');
			//console.log(data);
		},
		error: function() {
			console.log('FAILED!');
			//console.log(data);
		}
	}).done(function() {
		console.log('DONE!');
		//console.log(data);
	}).always(function() {
		console.log('ALWAYS!');
		//console.log(data);
	});
	//*/
}