ImThatGuy / ForumSearch

// ==UserScript==
// @name         ForumSearch
// @author       ImThatGuy
// @description  Barre de recherche alternative pour le forum 18-25 ans
// @include      http://www.jeuxvideo.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @version      0.2
// @icon         https://image.noelshack.com/fichiers/2017/50/1/1513022453-icon-sm.png
// @copyright    2017+, ImThatGuy
// @license      MIT
// ==/UserScript==

/*
ForumSearch. 2017.
Codé par ImThatGuy.

2017+, ImThatGuy.

Plugin pour JVCpremium : https://openuserjs.org/scripts/ImThatGuy/JVCpremium
11/12/17 -> Correction léger bug (affichage plugin)
*/

/*jshint multistr: true */
(function() {
    'use strict';

	var version = '0.2';

	function logConsole(text) {
		$('#consolePremium').append('<p style="margin:10px;">'+text+'</p>');
	}

	function listeSujet() {
        if (document.getElementsByName('search_in_forum').length === 0) {
            return false;
        } else {return true;}
    }

	$('head').append('<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pretty-checkbox@3.0/dist/pretty-checkbox.min.css"/>');

	setTimeout(function() {
		var JVCpremiumPlugin;
		if (document.getElementById("PREMIUM") !== null) {
			JVCpremiumPlugin  = true;
		} else {
			JVCpremiumPlugin  = false;
		}

		if (JVCpremiumPlugin) {
			logConsole('[INFO] Plugin ForumSearch '+version+' chargé.');
			$('#non-installer').remove(); // Correction 11/12/17
			// Appends à JVCpremium
			$('#plugin').append('<img width=70 height=70 src="https://image.noelshack.com/fichiers/2017/43/5/1509099907-forumsearch.png">\
<h5>Vous utilisez ForumSearch version '+version+'.</h5>\
<hr>');
		}

		if (listeSujet()) {
			$("#forum-right-col").prepend('<hr><p id="s-head" style="font-family: robotoboldcondensed,Arial,Helvetica,sans-serif; text-transform: uppercase; text-align:center">Forum Search v '+version+'</p><input placeholder="Rechercher dans le forum" style="width:60%" id="pre-search" type="text"> \
<input style="width:10%" id="pre-search-date" type="text" value="1"> \
<button style="width:20%" id="pre-search-btn">recherche</button> \
<div class="pretty p-default" style="font-size:11px;"> <input type="checkbox" id="largesearch" value="advancedsearch"> <div class="state"><label>Élargir ma recherche</label></div></div><br></br>');
		}
			function displayResults(title, snippet, link) {
				$(".topic-list").append('<div id="result-f"><a class="results-f" title="'+snippet+'" style="color:#262626" href="'+link+'">'+title+'</a></div>');
				$(".results-f").tooltip();
			}
			// Style
			GM_addStyle("#pre-search, #pre-search-date { border: 1px solid #ccc; padding: 5px; font-size:13px; outline:none; }\
#pre-search-btn { font-size:11px; border-radius: 2px; border: none; padding:8px; outline:none; transition: background-color 0.1s; background-color:rgba(26, 127, 204, 1); color: white }\
#pre-search-btn:hover { background-color: rgba(26, 127, 204, 0.74); }\
#result-f { margin: 8px; font-size: 15px; border: 1px solid #ccc; margin-bottom: 15px; padding: 5px; }\
#result-f:hover { background-color: #ededed }\
");
			// Google search (barre de recherche 403)
			function googleSearch(q, dr, checked) {

				if (q) {

					var KEY = "AIzaSyDmmoPdIlCG1_1BD2lCeDg4PymqdKorHIs";
					var CSE = "011712944612251553049:vy5xewvfar8";

					var ufh = "intitle:";
					if (checked) { ufh = ""; }

					var api = "https://www.googleapis.com/customsearch/v1?key=" + KEY + "&cx=" + CSE + "&q=" + ufh + encodeURIComponent(q) + "&dateRestrict=w" + dr;
					GM_xmlhttpRequest({
						method: "GET",
						url: api,
						headers: {
							"Content-Type": "application/x-www-form-urlencoded"
						}, onload: function (response) {
							var r = JSON.parse(response.responseText);
							if (r.items.length) {
								$(".topic-list").empty();
								for (var i=0; i<r.items.length; i++) {
									displayResults(
										r.items[i].title,
										r.items[i].snippet,
										r.items[i].link
									);
								}
							} else {
								console.log("Pas de résultats");
							}
						}
					});
				}

			}

			$("#pre-search-btn").click(function() {
				if ( $("#largesearch").is(":checked") ) {
					googleSearch($("#pre-search").val(), $("#pre-search-date").val(), true);
				} else {
					googleSearch($("#pre-search").val(), $("#pre-search-date").val(), false);
				}
			});

			if (listeSujet()) {
				if (!JVCpremiumPlugin) { $("#s-head").append(" (Vous n'utilisez pas JVCpremium)"); }
			}

	}, 1000);

})();