ExternalReview

This script will add external links to review websites like [commonsensemedia.org](http://www.commonsensemedia.org/).

Versione datata 18/02/2018. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         ExternalReview
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  This script will add external links to review websites like [commonsensemedia.org](http://www.commonsensemedia.org/).
//               Please see the links next to content ratings in the screen-shoot.
//               ### Credits:
//               - I like to thank [kayomani](https://greasyfork.org/en/users/19122-kayomani) for his awesome script [Plex External Player](https://greasyfork.org/en/scripts/13437-plex-external-player").
// @author       [email protected]
// @match        http*://*/web/index.html*
// @include      /^https?://.*:32400/web/index.html*
// @include      http://*:32400/web/index.html*
// @grant        GM_*
// @grant        unsafeWindow
// @run-at       document-end
// @require      http://code.jquery.com/jquery-3.2.1.min.js
// @locale       'en-US'

// ==/UserScript==
var AlreadyInjected = false;
var LastURL = "";

var Reset = function() {
    // I don't know of a better way to detect URL changes
    if (LastURL !== window.location.href) {
        LastURL = window.location.href;
        AlreadyInjected = false;
        console.log("CommonSenseMedia Reset");
        return true;
    }
    return false;
};

var GetTitle = function() {
    var title = jQuery("[class*='PrePlayPrimaryTitle-primaryTitle']")[0].textContent;
    return title;
};

var CSM_SEARCH="https://www.commonsensemedia.org/search/";
var PP_SEARCH="http://parentpreviews.com/search?keywords=";
var IMDB_SEARCH="http://www.imdb.com/find?ref_=nv_sr_fn&s=all&q=";

/** Escape RegExp special chars
 * @function escapeRegExp
 * @param string s
 * @return string escaped
 * @brief taken from Greasespot code snippets
 */
function escapeRegExp(s) {
	return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

var replaceURL = function() {
    Reset();

    if (!AlreadyInjected) {
        var titles = jQuery("[class*='PrePlayTertiaryTitle-tertiaryTitle']")[0];
        var a_elems = titles.getElementsByTagName("a");
        console.log("a_elems = " + a_elems.length);

        for (var i = 0; i < a_elems.length; i++) {
            this_href = a_elems[i].href;
            // console.log("CommonSenseMedia = " + this_href);
            if (this_href.indexOf("contentRating") !== -1) {
                console.log("CommonSenseMedia = Found!");
                // a_elems[i].href = CSM_SEARCH + encodeURIComponent(title);

                title = GetTitle();
                title_encoded = encodeURIComponent(title);
                title_plus = replaceAll(title, " ", "+");

                var cln = a_elems[i].cloneNode(true);
                cln.href = CSM_SEARCH + title_encoded;
                cln.textContent = "CSM";
                a_elems[i].parentNode.appendChild(cln);

                var cln2 = a_elems[i].cloneNode(true);
                cln2.href = PP_SEARCH + title_plus;
                cln2.textContent = "PP";
                a_elems[i].parentNode.appendChild(cln2);

                var cln3 = a_elems[i].cloneNode(true);
                cln3.href = IMDB_SEARCH + title_plus;
                cln3.textContent = "IMDB";
                a_elems[i].parentNode.appendChild(cln3);

                //console.log("new parent = " + a_elems[i].parentNode.innerHTML);
                AlreadyInjected = true;
                break;
            }
        }
    }
};


(function() {
    'use strict';

    // Your code here...
    if (Reset()) {

        // Bind buttons and check for new ones every 100ms
        setInterval(replaceURL, 500);
    }

})();