Ptt.cc: I am over 18

9/28/2023, 12:26:04 PM

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Ptt.cc: I am over 18
// @namespace   UserScripts
// @match       https://www.ptt.cc/*
// @grant       none
// @version     0.1.14
// @author      CY Fung
// @license     MIT
// @description 9/28/2023, 12:26:04 PM
// @require     https://cdn.jsdelivr.net/npm/[email protected]/dist/js.cookie.min.js
// @inject-into page
// @run-at      document-start
// ==/UserScript==
(() => {

  function getURL() {
    let p = location.href;
    if (p.includes('ask/over18?')) {
      let q = new URL(p);
      let from = q.searchParams.get('from');
      if (from && typeof from === 'string' && !from.includes('://')) return `${q.origin}${q.searchParams.get('from')}`;
    }
    return p;
  }

  let p1 = Cookies.get('over18', { domain: '' });
  Cookies.set('over18', '1', { expires: 36135, domain: '' });
  let p2 = Cookies.get('over18', { domain: '' });

  let redirection = '';
  if (p1 !== p2) {
    let url = getURL();
    redirection = url;
  } else if (p1 === '1' && p2 === '1') {
    let url = getURL();
    if (url !== location.href) {
      redirection = url;
    }
  }

  if (redirection) {
    let url = redirection;
    if (url.includes('?')) {
      url += '&' + Date.now();
    } else {
      url += '?' + Date.now();
    }
    location.replace(url);
  }

})();