IMDb details page links

Adds some links to IMDb details page

当前为 2015-09-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           IMDb details page links
// @namespace      IMDb.com
// @description    Adds some links to IMDb details page
// @include        *imdb.com/title/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
// @require        https://cdnjs.cloudflare.com/ajax/libs/jqModal/1.3.0/jqModal.min.js
// @grant          GM_getValue
// @grant          GM_setValue
// @grant          GM_registerMenuCommand
// @grant          GM_addStyle
// @version 2.0
// ==/UserScript==  

// CHANGELOG
// 2.0 - Make links modifiable
// 1.1 - Update URLs      

// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim
if(!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g,'');
    };
}

function defaultSites() {
    return [{title:"Google",           url:"http://www.google.com/search?q={title} {year}"},
            {title:"Subtitles",        url:"http://www.google.com/search?q={title} {year} subtitles"},
            {title:"YouTube",          url:"http://www.youtube.com/results?search_query={title} {year}"},
            {title:"iCheckMovies",     url:"http://www.icheckmovies.com/search/movies/?query={imdbid}"},
            {title:"RottenTomatoes",   url:"http://www.rottentomatoes.com/search/?search={title}"},
            {title:"HDBits",           url:"http://hdbits.org/browse2.php#film/dir=null&searchtype=film&actorfilm=film&search={imdbid}"},
            {title:"AsianDVDClub",     url:"http://asiandvdclub.org/browse.php?search={imdbid}&descr=1"},
            {title:"PassThePopCorn",   url:"http://passthepopcorn.me/torrents.php?searchstr={imdbid}"},
            {title:"KaraGarga",        url:"http://karagarga.in/browse.php?incldead=&d=&sort=&search={imdbidn}&search_type=imdb"},    
            {title:"Cinemageddon",     url:"http://cinemageddon.net/browse.php?search={imdbid}&proj=0"},
            {title:"AsiaTorrents",     url:"https://avistaz.to/torrents?in=1&search={title}&tags=&type=0&language=0&subtitle=0&discount=0&rip_type=0&video_quality=0&tv_type=0&uploader="},
            {title:"TehConnection",    url:"http://tehconnection.eu/torrents.php?searchstr={title}"},
            {title:"WhatTheMovie",     url:"http://whatthemovie.com/search?t=movie&q={imdbid}"},
            {title:"Mubi",             url:"http://www.google.com/search?q=site:mubi.com {title} {year}"},
            {title:"ListAL",           url:"http://www.google.com/search?q=site:listal.com {title} {year}"},
            {title:"HanCinema",        url:"http://www.hancinema.net/googlesearch.php?cx=partner-pub-1612871806153672%3A2t41l1-gajp&cof=FORID%3A10&ie=ISO-8859-1&hl=en&q={title}"},    
            {title:"Criticker",        url:"http://www.criticker.com/?st=movies&h={imdbid}&g=Go"},
            {title:"iCM Highest Rated",url:"http://themagician.host56.com/highestrated/search/#search={imdbid}"}];
}

/**
 * @brief Find header element
 * @return Header element or null if not found 
 */ 
function findHeader() {
    $header = null;
    if(location.href.indexOf("reference") === -1 && 
            location.href.indexOf("combined") === -1) {
        $overview = $("#overview-top");    
        if($overview !== null) {
            $header = $overview.find(".header:first");
        }                                                               
    }
    else {
        $header = $("#tn15title");                
    }
    return $header;
}

function parseConstants() {
    $header = findHeader();
    if($header.length === 0) {
        return null;
    }
    
    var matches = $header.text().replace(/(\r\n|\n|\r)/gm,"").match(/(.*)(?:[\s]+)(?:.*)\(.*([0-9]{4})/);
    var imdb_id = window.location.href.match(/(tt[0-9]+)/)[0];
    return {title: matches[1].trim(),
            year: matches[2].trim(),
            imdbid: imdb_id,
            imdbid_n: imdb_id.replace("tt", "")};
}

var App = {
    links: [],
    init: function() {
        App.buildLinks();
        
        // Add modal
        GM_addStyle('.jqmWindow {display: none; position: absolute; font-family: verdana, arial, sans-serif; ' +
        'background-color:#fff; color:#000; padding: 12px 30px; overflow-y: scroll; font-size: 14px} .jqmOverlay { background-color:#000 }');
        var cfgMainHtml = '<div id="dialog" class="jqmWindow"></div>';
        $(cfgMainHtml).css({
            top: '17%', left: '50%', marginLeft: '-400px', width: '800px', height: '450px'
        }).appendTo('body');                                 
        $('#dialog').jqm();
    },
    buildLinks: function() {
        var sites = GM_getValue("sites");
        if(sites !== null) {
            sites = eval(sites);
        }
        else {
            GM_setValue("sites", uneval(defaultSites()));
            sites = defaultSites();
        }
        
        $root = $("#pagecontent");
        $root.css("position", "relative");
        
        $("#linkbar").remove();
        $c = $('<div></div>');
        $c.attr("id", "linkbar");
        $c.css({width: "150px", padding: "15px",
                textAlign: "right", position: "absolute", top: "8px",
                left: "-170px", backgroundColor: "#fff"});
                                
        $root.append($c);
        
        // Render sites
        for(var i = 0; i < sites.length; ++i) {
            App.addSite(sites[i]);
        }
    }, 
    addSite: function(site) {
        var c = parseConstants();
        if(c === null) {
            return;
        }
        
        var url = site.url.replace("{title}", c.title)
                            .replace("{year}", c.year)
                            .replace("{imdbid}", c.imdbid)
                            .replace("{imdbidn}", c.imdbid_n);
        $root = $("#linkbar");                            
        $link = $("<a></a>");
        $link.attr("href", url);
        $link.text(site.title);
        $link.css({display: "block", marginBottom: "2px"});        
        $root.append($link);
    },
    displayEditor: function() {
        var sites = GM_getValue("sites");
        if(sites !== null) {
            sites = eval(sites);
        }
        
        var c = parseConstants();
        var out = '<div><p>{title} = ' + c.title + '</p>' 
                + '<p>{year} = ' + c.year
                + ' {imdbid} = ' + c.imdbid
                + ' {imdbidn} = ' + c.imdbid_n + '</p><table>';
        for(var i = 0; i < sites.length; ++i) {
            out += '<tr class="site-' + i + '"><td><a href="#" class="delete">Delete</a></td><td><input type="text" size="30" value="' 
                + sites[i].title + '"></td><td><input type="text" size="80" value="' 
                + sites[i].url + '"></td></tr>';
        }
        out += '</table><p><button id="add">Add new</button> <button id="restore">Restore defaults</button></p>';
        $("#dialog").html(out);
        $("#dialog").jqmShow();
        
        $("#dialog").on("change input paste", "tr[class^=site] input", App.saveEditor);
        
        $("#dialog").on("click", "a.delete", function(e){
            e.preventDefault();
            var response = window.confirm("Delete?");
            if(!response) {
                return;
            }
            
            $(this).parent().parent().remove();
            App.saveEditor();
        });
        
        $("#add", "#dialog").on("click", function() {
            var $table = $("table", "#dialog");
            $table.append('<tr class="site-' + $table.find("tr").length + '"><td><a href="#" class="delete">Delete</a></td>'
                                        + '<td><input type="text" size="30" value=""></td>'
                                        + '<td><input type="text" size="80" value=""></td></tr>');
        });
        
        $("#restore", "#dialog").on("click", function() {
            var response = window.confirm("Restore defaults? (This will remove all links!)");
            if(!response) {
                return;
            }
            
            GM_setValue("sites", uneval(defaultSites()));
            App.buildLinks();
            $("#dialog").jqmHide();
        });
    },
    saveEditor: function() {
        // Save result
        var $rows = $("tr", "#dialog");
        var mapped = $rows.map(function(index, elem){
            var $fields = $(elem).find("input");
            return {title: $fields.eq(0).val(), url: $fields.eq(1).val()}; 
        });
        
        GM_setValue("sites", uneval(mapped.get()));
        
        App.buildLinks(); 
    }
};

$(window).ready(App.init);

GM_registerMenuCommand("Edit sites", App.displayEditor);