-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredirect_to_ptt.user.js
More file actions
56 lines (55 loc) · 1.73 KB
/
redirect_to_ptt.user.js
File metadata and controls
56 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ==UserScript==
// @name PTT 以外的相關網站自動轉址到 ptt.cc
// @version 1.1.0
// @match *://www.pttweb.cc/bbs/*
// @match *://pttweb.tw/bbs/*
// @match *://ptthito.com/*
// @match *://disp.cc/b/*
// @match *://moptt.tw/p/*
// @match *://ptt-politics.com/*
// @run-at document-end
// @noframes
// ==/UserScript==
// @ts-check
'use strict';
let url = new URL(window.location.href);
switch (window.location.host) {
case "www.pttweb.cc":
{
url.pathname = `${url.pathname}.html`;
break;
}
case "ptthito.com":
{
url.pathname = url.pathname.replace(/\/(.+)\/(.+)\//, (str, board, id) => {
return `/bbs/${board[0].toUpperCase()}${board.substring(1)}/${id.replace(/-/g, ".").toUpperCase()}.html`;
});
break;
}
case "moptt.tw":
{
const path = url.pathname.split('/', 3)[2];
const board = path.split('.', 1)[0];
const file = path.slice(board.length + 1) + '.html';
url.pathname = `/bbs/${board}/${file}`;
break;
}
case "pttweb.tw":
case "disp.cc":
case "ptt-politics.com":
{
const links = document.querySelectorAll("a[href^='https://www.ptt.cc/bbs/']");
for (const a of links) {
if (a.parentElement?.textContent?.includes("※ 文章網址:")) {
const link = a.getAttribute("href");
if (link) {
url = new URL(link);
}
}
}
break;
}
}
url.protocol = "https:";
url.host = "www.ptt.cc";
window.location.href = url.toString();