zach14c / FastZone PlusPlus

// ==UserScript==
// @name         FastZone PlusPlus
// @namespace    https://fastzone.org/?32535
// @version      2024-09-29
// @description  FastZone PlusPlus
// @author       zach14c
// @match        https://fastzone.org/forum.php?mod=forumdisplay&fid=*
// @match        https://fastzone.org/forum.php?mod=viewthread&tid=*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @icon         https://www.google.com/s2/favicons?sz=64&domain=fastzone.org
// @grant             unsafeWindow
// @grant        GM_setClipboard
// @grant        GM_log
// @license      AGPL-3.0-or-later
// ==/UserScript==
const currentUrl = window.location.href;
var topTbodyDisplay = 'none';
var topTbody = [];
var currMode = 1;
var overMainFrame;
var docTitle = "";

function cleanLayout(){    
    const url = new URL(currentUrl);
    const id = url.searchParams.get('fid');
    if(id){
        var ruleDiv = document.getElementById("forum_rules_" + id);

        if(ruleDiv){
            $(ruleDiv).css('display','none');
        }
    }

    var tbodys = document.getElementsByTagName('tbody');
    for(let i=0; i < tbodys.length; i++){
        var tbody = tbodys[i];
        if(tbody && (tbody.id.startsWith('mb_data_') || tbody.id.startsWith('stickthread_'))){
            $(tbody).css('display',topTbodyDisplay);
            topTbody.push(tbody);
        }
    }

    var $favs = $('.num > em');
    $favs.each(function(){
        var $em = $(this);
        var html = $em.html();
        var nums = html.match(/\d+/);
        if(nums){
            var numOfLovs = parseInt(nums[0]);
            const topicThs = $em.parent().siblings('.common');
            var bakColor = '';
            if(numOfLovs>=200){
                bakColor = '#c7005c';
            }else if(numOfLovs>=100){
                bakColor = 'rgb(0 60 199)';
            }
            if(topicThs.length > 0){
                var $th = $(topicThs[0]);
                $th.css('background','radial-gradient('+bakColor+', transparent)');
            }
        }
    });
};

function addToolButton(){
    var $switchButton = $('<a>', {
        class: 'menu-btn showmenu xi2', text: '切換3個月',
        href: '#',
        click: function(event){
            event.preventDefault();
            navigateTo30DaysThreads();
        }
    });
    var $toggleButton = $('<a>', {
        class: 'menu-btn showmenu xi2', text: '切換置頂',
        href: '#',
        click: function(event){
            event.preventDefault();
            toggleVisibleTbodys();
        }
    });
    var $space1 = $('<span>', { id:'sp1', css:{'margin-right':'10px'} });
    var $space2 = $('<span>', { id:'sp2', css:{'margin-right':'10px'} });
    $('#filter_special').before($switchButton).before($space1);
    if(topTbody.length > 0){
        $switchButton.before($toggleButton).before($space2);
    }
}
function navigateTo30DaysThreads(){
    const switchUrl = currentUrl + "&orderby=dateline&filter=dateline&dateline=7948800";
    window.location.href = switchUrl;
}
function toggleVisibleTbodys(){
    if (topTbodyDisplay === "none"){
        topTbodyDisplay = 'table-row-group';
    } else {
        topTbodyDisplay = 'none';
    }
    for(let i=0; i < topTbody.length; i++){
        var tbody = topTbody[i];
        $(tbody).css('display',topTbodyDisplay);
    }
}
function createSearchMenu(){
    const popupMenu = document.createElement('div');
    popupMenu.id = "fastzonePlusMenu";
    popupMenu.style.position = 'absolute';
    popupMenu.style.background = '#000';
    popupMenu.style.border = '1px solid #ccc';
    popupMenu.style.padding = '5px';
    popupMenu.style.display = 'none';
    popupMenu.style.zIndex = '9999';

    const copyClipboard = document.createElement('div');
    copyClipboard.textContent = '複製';
    copyClipboard.style.cursor = 'pointer';
    copyClipboard.style.padding = '5px';
    copyClipboard.style.borderBottom = '1px solid #ccc';

    const googleSearch = document.createElement('div');
    googleSearch.textContent = 'Google';
    googleSearch.style.cursor = 'pointer';
    googleSearch.style.padding = '5px';
    googleSearch.style.borderBottom = '1px solid #ccc';

    const youtubeSearch = document.createElement('div');
    youtubeSearch.textContent = 'YouTube';
    youtubeSearch.style.cursor = 'pointer';
    youtubeSearch.style.padding = '5px';

    popupMenu.appendChild(copyClipboard);
    popupMenu.appendChild(googleSearch);
    popupMenu.appendChild(youtubeSearch);
    document.body.appendChild(popupMenu);

    document.addEventListener('click', function(event) {
        var popupMenu = document.getElementById("fastzonePlusMenu");

    });

    document.addEventListener('mouseup', function(event) {
        const selectedText = window.getSelection().toString().trim();
        if (selectedText.length > 0) {
            popupMenu.style.left = event.pageX + 'px';
            popupMenu.style.top = event.pageY + 'px';
            popupMenu.style.display = 'block';

            googleSearch.onclick = () => {
                window.open("https://www.google.com/search?q=" + encodeURIComponent(selectedText), '_blank');
                popupMenu.style.display = 'none';
            };

            youtubeSearch.onclick = () => {
                window.open("https://www.youtube.com/results?search_query=" + encodeURIComponent(selectedText), '_blank');
                popupMenu.style.display = 'none';
            };

            copyClipboard.onclick = () => {
                GM_setClipboard(selectedText);
            }
        } else {
            popupMenu.style.display = 'none';
        }
    });
}
function switchMainFrame(){
    currMode = currMode*-1;

    if(currMode == -1){
        docTitle = document.title;
    }
    var displayCSS = currMode==-1?'block':'none';
    var scrollCSS = currMode==-1?'hidden':'auto';
    var dispTitle = currMode==-1?'無限討論區':docTitle;
    if(overMainFrame){
        overMainFrame.style.display = displayCSS;
    } else {
        overMainFrame = document.createElement('iframe');
        overMainFrame.src = 'https://fastzone.org/';
        overMainFrame.style.position = 'fixed';
        overMainFrame.style.top = 0;
        overMainFrame.style.left = 0;
        overMainFrame.style.width = '100%';
        overMainFrame.style.height = '100%';
        overMainFrame.style.zIndex = 9999;
        overMainFrame.style.border = 'none';
        document.body.appendChild(overMainFrame);
        overMainFrame.style.display = displayCSS;
    }
    document.body.style.overflowY = scrollCSS;
    document.title = dispTitle;
}
(function() {
    const url = new URL(currentUrl);
    const mod = url.searchParams.get('mod');
    window.addEventListener('load',function(){
        if( mod === "forumdisplay" ){
            cleanLayout();
            addToolButton();
        }
    });
    window.addEventListener('keydown',function(event){
        if(event.key === 'Escape'){
            switchMainFrame();
        }
    });
    if ( mod === "viewthread" ){
        createSearchMenu();
    }
})();