Auto All Page

Otomatis menampilkan semua halaman artikel berita dalam 1 page

目前為 2021-07-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Auto All Page
// @version       1.8.0
// @author        reforget-id
// @namespace     autoallpage
// @icon          https://www.iconsdb.com/icons/download/orange/pages-1-256.png
// @homepage      https://github.com/reforget-id/AutoAllPage
// @description   Otomatis menampilkan semua halaman artikel berita dalam 1 page
// @include       http*://*.detik.com/*
// @include       http*://*.kompas.com/*
// @include       http*://*.tribunnews.com/*
// @include       http*://*.merdeka.com/*
// @include       http*://*.suara.com/*
// @include       http*://*.matamata.com/*
// @include       http*://*.sindonews.com/read/*
// @include       http*://*.inews.id/*
// @include       http*://*.grid.id/read/*
// @include       http*://*.bolasport.com/read/*
// @include       http*://*.motorplus-online.com/read/*
// @include       http*://*.gridoto.com/read/*
// @include       http*://*.pikiran-rakyat.com/*
// @include       http*://*.kontan.co.id/news/*
// @include       http*://akurat.co/*
// @include       http*://*.kompasiana.com/*
// @include       http*://*.cnbcindonesia.com/*
// @include       http*://*.republika.co.id/*
// @include       http*://creativedisc.com/*
// @include       http*://*.okezone.com/read/*
// @include       http*://*.viva.co.id/*
// @include       http*://*.kompas.tv/*
// @include       http*://*.wartaekonomi.co.id/*
// @include       http*://herstory.co.id/*
// @include       http*://*.sonora.id/*
// @include       http*://*.tvonenews.com/*
// @include       http*://*.intipseleb.com/*
// @include       http*://*.sahijab.com/*
// @include       http*://*.jagodangdut.com/*
// @include       http*://*.100kpj.com/*
// @grant         GM_xmlhttpRequest
// @run-at        document-start
// ==/UserScript==

'use strict';

(() => {
    // http*://*.jpnn.com/news/*

    let mainPage
    const url = window.location.href
    const log = '[AutoAllPage]'

    const redirectRegex = {
        detik: /(?<=^.+\.detik\.com\/[a-z-]+\/d-\d{7,}\/.+)((?<!\?.*|\/\d*)|\?.*(?<!\?single=1)|\/\d*)$/,
        kompas: /(?<=^.+\.kompas\.com\/([a-z-]+\/|)read\/\d{4}\/\d{2}\/\d{2}\/\d+\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all(#page\d+|))|\/)$/,
        tribun: /(?<=^.+.tribunnews\.com\/([a-z-]+\/|)\d{4}\/\d{2}\/\d{2}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        merdeka: /(?<=^.+\.merdeka\.com\/[a-z-]+\/.+\.html)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        suara: /(?<=^.+\.(suara|matamata)\.com\/[a-z-]+\/\d{4}\/\d{2}\/\d{2}\/\d{6,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        sindo: /(?<=^.+\.sindonews\.com\/read\/\d{6,}\/\d{2,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?showpage=all)|\/\d*)$/,
        inews: /(?<=^.+\.inews\.id\/(berita|[a-z-]+\/[a-z-]+)\/.+)((?<!\?.*|\/(all.*|\d*))|(\/all.+)|\/\d*|\?.*)$/,
        grid: /(?<=^.+\.(grid\.id|(motorplus-online|gridoto|bolasport)\.com)\/read\/\d{8,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        pr: /(?<=^.+\.pikiran-rakyat\.com\/[a-z-]+\/pr-\d{7,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        kontan: /(?<=^.+\.kontan\.co\.id\/news\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        akurat: /(?<=^.+akurat\.co\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        kompasiana: /(?<=^.+\.kompasiana\.com\/.+\/[a-z0-9]{24}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all(#sectionall|))|\/)$/,
        viva: /(?<=^.+\.viva\.co\.id\/([a-z-]+(\/[a-z-]+|))\/\d{6,}-.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        cnbc: /(?<=^.+\.cnbcindonesia\.com\/[a-z-]+\/\d{14}-\d{1,}-\d{4,}\/.+)(\/([2-9]|\d{2})(\?.+|))$/,
        republika: /(?<=^.+\.republika\.co\.id\/berita\/[a-z0-9]+\/.+)(-part\d+.*)$/,
        jpnn: /(?<=^.+\.jpnn\.com\/news\/.+)(\?.+=.+)$/,
        cd: /(?<=^.+creativedisc\.com\/\d{4}\/\d{2}\/.+)(\/\d+\/.*)$/,
        okezone: /(?<=^.+\.okezone\.com\/read\/\d{4}\/\d{2}\/\d{2}\/\d{1,}\/\d{6,}\/.+)(\?page=([2-9]|\d{2}).*)$/,
        kompastv: /(?<=^.+\.kompas\.tv\/article\/\d{4,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        warkom: /(?<=^.+\.wartaekonomi\.co\.id\/read\d{5,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/ ,
        herstory: /(?<=^.+herstory\.co\.id\/read\d{4,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        sonora: /(?<=^.+\.sonora\.id\/read\/\d{8,}\/.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/,
        tvone: /(?<=^.+\.(tvonenews|intipseleb|sahijab|jagodangdut|100kpj)\.com\/([a-z-]+(\/[a-z-]+|))\/\d{4,}-.+)((?<!\?.*|\/)|\?.*(?<!\?page=all)|\/)$/
    }

    const xhrRegex = {
        cnbc: /(^.+\.cnbcindonesia\.com\/[a-z-]+\/\d{14}-\d{1,}-\d{4,}\/.+)/,
        republika: /(^.+\.republika\.co\.id\/berita\/[a-z0-9]+\/.+)/,
        jpnn: /(^.+\.jpnn\.com\/news\/.+)/,
        cd: /(^.+creativedisc\.com\/\d{4}\/\d{2}\/.+)/,
        okezone: /(^.+\.okezone\.com\/read\/\d{4}\/\d{2}\/\d{2}\/\d{1,}\/\d{6,}\/.+)/
    }

    for (let i in redirectRegex) {
        let urlMatcher = url.match(redirectRegex[i])
        if (urlMatcher) {
            redirector(i)
            break
        }
    }

    function redirector(patternName) {
        let replacer, newUrl

        if (patternName == 'detik') {
            replacer = '?single=1'
        } else if (patternName == 'sindo') {
            replacer = '?showpage=all'
        } else if (patternName == 'inews') {
            replacer = '/all'
        } else if (
            patternName == 'cnbc' ||
            patternName == 'republika' ||
            patternName == 'jpnn' ||
            patternName == 'cd' ||
            patternName == 'okezone') {
            replacer = ''
        } else {
            replacer = '?page=all'
        }

        newUrl = url.replace(redirectRegex[patternName], replacer)
        console.log(`EXECUTE [${patternName}] REDIRECT`)
        window.location.href = newUrl
        return
    }

    window.addEventListener('DOMContentLoaded', () => {
        console.log(log, 'DOM fully loaded and parsed')

        for (let i in xhrRegex) {
            let urlMatcher = url.match(xhrRegex[i])
            if (urlMatcher) {
                prepareXhr(i)
                break
            }
        }
    })

    function prepareXhr(patternName) {
        switch(patternName) {
            case 'cnbc' :
                cnbcXhr()
                break
            case 'republika' :
                republikaXhr()
                break
            case 'jpnn' :
                jpnnXhr()
                break
            case 'cd' :
                cdXhr()
                break
            case 'okezone' :
                okezoneXhr()
        }

        /*
        if (patternName == 'cnbc') {
            cnbcXhr()
        } else if (patternName == 'republika') {
            republikaXhr()
        } else if (patternName == 'jpnn') {
            jpnnXhr()
        }*/
    }

    function findPagination(className) {
        const paging = document.getElementsByClassName(className)[0]
        if (!paging) console.log(log, 'Pagination is not found')
        return paging
    }

    function createXhr(url, i, find) {
        console.log(log, `Creating XHR request for page ${i+1}`)
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: url,
                overrideMimeType: 'text/html; charset=UTF-8',
                responseType: 'document',
                binary: false,
                timeout: 0,
                withCredentials: true,
                onerror: () => {
                    reject(console.log(log, 'Failed to create XHR request'))
                },
                onload: async (res) => {
                    let text
                    const hostname = window.location.hostname

                    if (hostname.includes('jpnn.com') || hostname.includes('creativedisc.com')) {
                        text = res.response.querySelector(find)
                    } else {
                        text = res.response.getElementsByClassName(find)[0]
                    }

                    if (text) {
                        console.log(log, `Success get text of page ${i+1} from XHR`)
                        if (hostname.includes('okezone.com')) {
                            mainPage.after(text)
                        } else {
                            mainPage.appendChild(text)
                        }
                        resolve(console.log(log, `Append page ${i+1} to main page`))
                    } else {
                        reject(console.log(log, 'Failed to get text XHR'))
                    }
                }
            })
        })
    }

    async function cnbcXhr() {
        const pagination = findPagination('paging-circle')
        if (!pagination) return

        const pagingParent = pagination.parentNode
        pagingParent.style.display = 'none'
        pagingParent.previousElementSibling.style.display = 'none'
        mainPage = document.getElementsByClassName('detail_text')[0].parentNode

        for (let i = 1; i < pagination.childElementCount; i++) {
            let href = pagination.children[i].getAttribute('href')
            await createXhr(href, i, 'detail_text')
        }
    }

    async function republikaXhr() {
        const pagination = findPagination('pagination')
        if (!pagination) return

        const pageChildren = pagination.getElementsByTagName('a')
        mainPage = document.getElementsByClassName('teaser_detail')[0]
        pagination.style.display = 'none'

        for (let i = 1; i < pageChildren.length - 1; i++) {
            let href = pageChildren[i].getAttribute('href')
            await createXhr(href, i, 'artikel')
        }

        const bacaJuga = document.getElementsByClassName('baca-juga')
        for (let i = 0; i < bacaJuga.length; i++) {
            bacaJuga[i].remove()
            i--
        }
    }

    async function jpnnXhr() {
        const pagination = findPagination('pagination')
        if (!pagination) return

        const pageChildren = pagination.getElementsByTagName('a')
        mainPage = document.querySelector('[itemprop=articleBody]')
        pagination.style.display = 'none'

        for (let i = 1; i < pageChildren.length - 1; i++) {
            let href = pageChildren[i].getAttribute('href')
            await createXhr(href, i, '[itemprop=articleBody]')
        }
    }

    async function cdXhr() {
        const pagination = findPagination('post-nav-links')
        if (!pagination) return

        const pageChildren = pagination.getElementsByTagName('a')
        mainPage = document.querySelector('[itemprop=articleBody]').parentNode
        pagination.style.display = 'none'

        for (let i = 0; i < pageChildren.length; i++) {
            let href = pageChildren[i].getAttribute('href')
            await createXhr(href, i+1, '[itemprop=articleBody]')
        }
    }

    async function okezoneXhr() {
        const pagination = document.getElementsByClassName('paging')
        if (!pagination.length) {
            console.log(log, 'Pagination is not found')
            return
        }

        const pageChildren = pagination[0].getElementsByTagName('a')[0]
        const href = pageChildren.getAttribute('href')
        mainPage = document.getElementsByClassName('read')[0]

        if (href === '#') return
        
        await createXhr(href, 1, 'read')

        const detailTag = document.getElementsByClassName('detail-tag')
        for (let i = 0; i < pagination.length; i++) {
            pagination[i].style.display = 'none'
            detailTag[i].style.display = 'none'
        }
    }

})()