TVC-TVT Integration

Click on episodes to get to the torrents page

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         TVC-TVT Integration
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Click on episodes to get to the torrents page
// @author       M.Seven
// @match        *://www.pogdesign.co.uk/cat/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';
    var mapping = {};

    var cache = GM_getValue('mapping');

    if (cache) {
        mapping = cache;
        console.info('loading tv torrents list from cache');
    } else {
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://freshon.tv/browse.php",
            onload: function(response) {
                var arr = response.responseText.split('<select')[1].split('<option');
                arr.forEach(function(val){
                    var m = val.match(/value="([0-9]+)".*?>(.*?)<.*?/i);
                    if (m) {
                        mapping[m[2]] = m[1];
                    }
                });
                console.info('finished loading tv torrents list');
                GM_setValue('mapping',mapping);
                console.info('writing tv torrents list to cache');
            }
        });
    }
//    $('div.ep.info span p span').click(function(event){
//        var div = $(this).parent().parent().parent();
//        var name = $(div).find('a:first-child').html();
    $('div.ep.info').click(function(event){
        var name = $(this).find('a:first-child').html();
        var id = mapping[name];
        if (!id) {
            var stripName = name.replace(/\W+/g, ' ').toLowerCase().trim(), bestMatch, maxWords=0;
            for (var key in mapping) {
                if (mapping.hasOwnProperty(key)) {
                    var wordCount = 0, stripKey = key.replace(/\W+/g, '').toLowerCase();
                    stripName.split(' ').forEach(function (word) {
                        if (stripKey.indexOf(word) != -1) {
                            wordCount++;
                        }
                    });
                    if (wordCount>maxWords) {
                        maxWords = wordCount;
                        bestMatch = key;
                    }
                }
            }
            id = mapping[bestMatch];
            console.info('best match:',bestMatch,'| words:',maxWords);
        } else {
            console.info('exact match:',name);
        }
        window.open('https://freshon.tv/browse.php?cat='+id);
        event.preventDefault();
    });
})();