[Chrome] 使用 Tampermonkey 在楓林 TV 網頁增加下一集連結
最近在看楓林 TV 網頁上提供的柯南動畫,
不過有個缺點是,當一集播放完畢時,
它不會自動播放下一集 (而是重播目前那一集),
因此就得捲到頁面下方,去找出下一集的按鈕來點:

當然這也不是什麼大問題,
只是你要記得現在是第幾集,然後去找下一集的按鈕…
身為懶惰的工程師,還是用 Tampermonkey 來做個「下一集」的連結吧~
程式如下,流程很簡單,
先找出頁面裡所有的播放連結 (由新到舊),
然後依序檢查連結是否就是目前這一集的網址,
如果是的話,那它的前一個連結就會是下一集了:
// ==UserScript==
// @name         Conan: Next episode
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       ephrain
// @match        https://fenglin.tv/play/Jllol/*
// @require      https://code.jquery.com/jquery-3.4.1.slim.min.js
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    let nextEpisodeLink = null;
    let allLinks = $('a[href^="/play/"]');
    // The links are in descending order, such as 5, 4, 3, 2, 1.
    for (let i = 0; i < allLinks.length; i++) {
        // When the link of current episode is found,
        //   previous stored link is the next episode link.
        if (location.href === allLinks[i].href) {
            console.log(`Next episode link is ${nextEpisodeLink}`);
            $('h1.title').append(`<a href="${nextEpisodeLink}" style="text-decoration: underline">>>下一集</a>`);
            break;
        }
        nextEpisodeLink = allLinks[i].href;
    }
})();
因為正好在學 JavaScript ES6,
學以致用,使用了宣告區域變數用的 let,
和在字串中引用變數內容的 `${variable}` 語法,感覺挺不錯的~
當頁面載入後,我們的程式就會自動加上一個「下一集」的連結,
點下就會連到柯南下一集的頁面囉:

(本頁面已被瀏覽過 392 次)