CouchPotato UserScript by Wolph

Add movies like a real CouchPotato

// ==UserScript==
//
// If you can read this, you need to enable or install the Greasemonkey add-on
// for firefox If you are using Chrome, download this file and drag it to the
// extensions tab Other browsers, use the bookmarklet
//
// Note that this script is forked from the original CouchPotato version, for
// support contact wolph@wol.ph or go to:
// https://github.com/WoLpH/couchpotato-userscript
//
// @name        CouchPotato UserScript by Wolph
// @namespace   wol.ph
// @description Add movies like a real CouchPotato
// @include     http://*
// @include     https://*
// @exclude     http://trakt.tv/movie/*/*
// @exclude     http://*.trakt.tv/movie/*/*
// @exclude     *://www.rottentomatoes.com/m/*/*/
// @version     1
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_registerMenuCommand
// ==/UserScript==

var supported_urls = [
    'http://www.youtheater.com/view.php?id=*',
    'http://youtheater.com/view.php?id=*',
    'http://www.sratim.co.il/view.php?id=*',
    'http://sratim.co.il/view.php?id=*',
    '*://filmcentrum.nl/films/*',
    '*://www.filmstarts.de/kritiken/*',
    'http://www.criticker.com/film/*',
    'http://www.themoviedb.org/movie/*',
    'http://whiwa.net/stats/movie/*',
    'http://trakt.tv/movie/*',
    'http://*.trakt.tv/movie/*',
    'http://www.flickchart.com/movie/*',
    '*://www.reddit.com/r/Ijustwatched/comments/*',
    'http://www.allocine.fr/film/*',
    '*://www.rottentomatoes.com/m/*/',
    'http://www.filmweb.pl/film/*',
    '*://letterboxd.com/film/*',
    'http://*.moviemeter.nl/film/*',
    'http://moviemeter.nl/film/*',
    '*://movies.io/m/*',
    'http://*.sharethe.tv/movies/*',
    'http://sharethe.tv/movies/*',
    '*://*.imdb.com/title/tt*',
    '*://imdb.com/title/tt*',
    'http://trailers.apple.com/trailers/*'
];

function getHost() {
    return GM_getValue('host', 'http://localhost:5000');
}

function setHost() {
    try{
        var api = unsafeWindow.Api.options.url;
        var host = (document.location + '');
        var host = host.replace(/(https?:\/\/[^\/]+)(\/.*|$)/, '$1');
        GM_setValue('host', host);
        GM_setValue('api', api);
        console.log('Set host to: ' + host);
        console.log('Set api to: ' + api);
    }catch(e){
        console.error(e);
        if(window.confirm('Unable to detect this as a CouchPotato host. '
            + 'Would you like to be redirect to the bugreport page?')){
            document.location.href = 'https://github.com/WoLpH/couchpotato-userscript/issues/';
        }
    }
}

function getAPI() {
    return GM_getValue('api', '');
}

function globToRegex(str) {
  str = str.replace(/[\-\[\]\/\{\}\(\)\+\?\.\\\^\$\|]/g, "\\$&");
  str = str.replace(/\*/g, '.\*');
  return new RegExp(str, 'i');
}

var create = function() {
    var A, B;
    switch (arguments.length) {
    case 1:
        A = document.createTextNode(arguments[0]);
        break;
    default:
        A = document.createElement(arguments[0]);
        B = arguments[1];

        for (var b in B) {
            if (b.indexOf("on") == 0) {
                A.addEventListener(b.substring(2), B[b], false);
            }
            else if (",style,accesskey,id,name,src,href,which".indexOf(","
                    + b.toLowerCase()) != -1) {
                A.setAttribute(b, B[b]);
            }
            else{
                A[b] = B[b];
            }
        }
        for ( var i = 2, len = arguments.length; i < len; ++i) {
            A.appendChild(arguments[i]);
        }
    }
    return A;
}

function addStyle(css) {
    var head = document.getElementsByTagName('head')[0],
        style = document.createElement('style');
    if (!head)
        return;

    style.type = 'text/css';
    style.textContent = css;
    head.appendChild(style);
}

// Styles
function addStyles(){
    addStyle('\
        #cp_popup { \
            font-family: "Helvetica Neue", Helvetica, Arial, Geneva, sans-serif; \
            -moz-border-radius: 6px 0px 0px 6px; \
            -webkit-border-radius: 6px 0px 0px 6px; \
            border-radius: 6px 0px 0px 6px; \
            -moz-box-shadow: 0 0 20px rgba(0,0,0,0.5); \
            -webkit-box-shadow: 0 0 20px rgba(0,0,0,0.5); \
            box-shadow: 0 0 20px rgba(0,0,0,0.5); \
            position:fixed; \
            z-index:20000; \
            bottom:0; \
            right:0; \
            font-size:15px; \
            margin: 20px 0; \
            display: block; \
            background:#4E5969; \
        } \
        #cp_popup.opened { \
            width: 492px; \
        } \
        #cp_popup a#add_to { \
            cursor:pointer; \
            text-align:center; \
            text-decoration:none; \
            color: #000; \
            display:block; \
            padding:5px 0 5px 5px; \
        } \
        #cp_popup a#close_button { \
            cursor:pointer; \
            float: right; \
            padding:120px 10px 10px; \
        } \
        #cp_popup a img { \
            vertical-align: middle; \
        } \
        #cp_popup a:hover { \
            color:#000; \
        } \
        #cp_popup iframe{ \
            background:#4E5969; \
            margin:6px 0 2px 6px; \
            height:140px; \
            width:450px; \
            overflow:hidden; \
            border:none; \
        } \
    ');
}

function osd() {
    addStyles();
    var cp_icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAADHmlDQ1BJQ0MgUHJvZmlsZQAAeAGFVN9r01AU/tplnbDhizpnEQk+aJFuZFN0Q5y2a1e6zVrqNrchSJumbVyaxiTtfrAH2YtvOsV38Qc++QcM2YNve5INxhRh+KyIIkz2IrOemzRNJ1MDufe73/nuOSfn5F6g+XFa0xQvDxRVU0/FwvzE5BTf8gFeHEMr/GhNi4YWSiZHQA/Tsnnvs/MOHsZsdO5v36v+Y9WalQwR8BwgvpQ1xCLhWaBpXNR0E+DWie+dMTXCzUxzWKcECR9nOG9jgeGMjSOWZjQ1QJoJwgfFQjpLuEA4mGng8w3YzoEU5CcmqZIuizyrRVIv5WRFsgz28B9zg/JfsKiU6Zut5xCNbZoZTtF8it4fOX1wjOYA1cE/Xxi9QbidcFg246M1fkLNJK4RJr3n7nRpmO1lmpdZKRIlHCS8YlSuM2xp5gsDiZrm0+30UJKwnzS/NDNZ8+PtUJUE6zHF9fZLRvS6vdfbkZMH4zU+pynWf0D+vff1corleZLw67QejdX0W5I6Vtvb5M2mI8PEd1E/A0hCgo4cZCjgkUIMYZpjxKr4TBYZIkqk0ml0VHmyONY7KJOW7RxHeMlfDrheFvVbsrj24Pue3SXXjrwVhcW3o9hR7bWB6bqyE5obf3VhpaNu4Te55ZsbbasLCFH+iuWxSF5lyk+CUdd1NuaQU5f8dQvPMpTuJXYSWAy6rPBe+CpsCk+FF8KXv9TIzt6tEcuAcSw+q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EJ1TsYKtyTb+q9M/0B2368euAK7Qc1LRGYw+2HBO/LeYn1lOHtAleh+7dF1xj4+vJInnxLgB0JU3mqQTDeK/ux/rlWso/+fBDV9BjssNTf8AmPnhttjsZCIAAAAASUVORK5CYII=';
    var close_img = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAAA5ElEQVR42tRTQYoEIQwsl/2Bl3gQoY9eBKEf5kvyG8G7h4Z+S38gIu5lp5lZ2R7YPm1BDhZJSFWiGmPgDj5wE7cbfD4/mBkAHprUj9yTTyn9OsGIMSLG+Fxwxc8SiAi9d4QQHskjhIDeO4jorQcq5wwiQmsN3nt479FaAxEh5zxJmyZIKalSClprL1FKQUpJXZr4DBH52xqZeRhjICKw1sJaCxGBMQbMPN41GFpriAicc6i1otYK5xxEBFrraQuThGVZAADbtp2amXms6woAOI7j0gO17/t5MN+HNfEvBf//M30NAKe7aRqUOIlfAAAAAElFTkSuQmCC';

    var navbar, newElement;

    var createApiUrl = function(url) {
        return getHost() + getAPI() + "/userscript/?url=" + escape(url)
    };

    var iframe = create('iframe', {
        'src': createApiUrl(document.location.href),
        'frameborder': 0,
        'scrolling': 'no'
    });

    var popup = create('div', {
        'id': 'cp_popup'
    });

    var onclick = function() {

        // Try and get imdb url
        try {
            var regex = new RegExp(/tt(\d{7})/);
            var imdb_id = document.body.innerHTML.match(regex)[0];
            if (imdb_id)
                iframe.setAttribute('src', createApiUrl('http://imdb.com/title/'+imdb_id+'/'))
        }
        catch(e) {}

        popup.innerHTML = '';
        popup.setAttribute('class', 'opened');
        popup.appendChild(create('a', {
            'innerHTML': '<img src="' + close_img + '" />',
            'id': 'close_button',
            'onclick': function() {
                popup.innerHTML = '';
                popup.appendChild(add_button);
                popup.setAttribute('class', '');
            }
        }));
        popup.appendChild(iframe)
    }

    var add_button = create('a', {
        'innerHTML': '<img src="' + cp_icon + '" />',
        'id': 'add_to',
        'onclick': onclick
    });
    popup.appendChild(add_button);

    document.body.parentNode.insertBefore(popup, document.body);

    // Auto fold open
    if(document.body.getAttribute('cp_auto_open'))
    	onclick()
}

function main(){
    var i = supported_urls.length;
    while(i--){
        var re = globToRegex(supported_urls[i]);
        if(re.test(document.location.href)){
            return osd();
        }
    }
}

if (window.top == window.self) {  // Only run on top window
    main();
}

GM_registerMenuCommand('Set this host as your CouchPotato host', setHost);