巴哈姆特哈拉開圖器 fixed

直接顯示巴哈姆特哈啦區文章的圖片和影片。跳過站外連結警告。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        巴哈姆特哈拉開圖器 fixed
// @description	直接顯示巴哈姆特哈啦區文章的圖片和影片。跳過站外連結警告。
// @namespace   eight04.blogspot.com
// @include     http://forum.gamer.com.tw/*
// @include     https://forum.gamer.com.tw/*
// @version     1.3.0
// @author		akiratw
// @contributor	eight04 <[email protected]> (https://github.com/eight04)
// @homepage	https://github.com/eight04/baha-embed-images
// @supportURL	https://github.com/eight04/baha-embed-images/issues
// @license		MIT
// @compatible	firefox
// @compatible	chrome
// @grant		none
// ==/UserScript==

/* globals getCookie */

if (getCookie("ckForumLegend") == "yes") {
	displayImage();
	displayYouTube();
}
redirectLinks();

function redirectLinks() {
    var links = document.querySelectorAll('a'),
        regex = /.*redir\.php\?url=(.+)/i;

    for (var i = links.length - 1; i >= 0; i--) {
        var link = links[i],
            url = link.getAttribute('href');

        if (regex.test(url)) {
            link.setAttribute(
                'href',
                decodeURIComponent(url.replace(regex, '$1'))
            );
        }
		if (link.onclick && link.matches("#BH-master article a")) {
			link.onclick = null;
		}
    }
}

function displayYouTube() {
    var links = document.querySelectorAll('a'),
        regex = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#&?]*).*/i;

    for (var i = links.length - 1; i >= 0; i--) {
        var link = links[i],
            url = link.getAttribute('href');
			
		if (!url) continue;
		
		var matches = url.match(regex);

        if (matches) {
            url = '//www.youtube.com/embed/' + matches[1];
            var iframe = document.createElement('iframe');
            iframe.setAttribute('width', '560');
            iframe.setAttribute('height', '315');
            iframe.setAttribute('frameborder', '0');
            iframe.setAttribute('allowfullscreen', 'true');
            iframe.setAttribute('src', url);
            link.parentNode.replaceChild(iframe, link);
        }
    }
}

function displayImage() {
	var links = document.querySelectorAll('a'),
		limit = 3;
		
	links = Array.from(links);
	
	while (limit--) {
		deque();
	}
	
	function deque() {
		var link = links.shift();
		
		if (!link) return;
		
        var src = link.id || link.href;
            
		if (!/(\.gif|\.jpg|\.jpeg|\.png|\.bmp)/i.test(src) || !/圖片|開圖/.test(link.textContent)) {
			return deque();
		}
		
		var img = new Image;
		img.src = src;
		img.addEventListener("load", handleLoad);
		img.addEventListener("error", handleLoad);
		
		link.innerHTML = "";
		link.onclick = null;
		link.target = "_blank";
		link.appendChild(img);
		link.classList.remove("image-load");
	}
	
	function handleLoad(e) {
		e.target.removeEventListener("load", handleLoad);
		e.target.removeEventListener("error", handleLoad);
		return deque();
	}
}