-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeiboUtils.user.js
71 lines (61 loc) · 2.26 KB
/
WeiboUtils.user.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// ==UserScript==
// @id weibo-utils
// @name weibo utils
// @category utils
// @version 1.0.0
// @updateURL https://raw.githubusercontent.com/zunsthy/userscripts/master/WeiboUtils.meta.js
// @downloadURL https://raw.githubusercontent.com/zunsthy/userscripts/master/WeiboUtils.user.js
// @author ZunSThy <zunsthy@gmail.com>
// @description some quick utils method for weibo.com/m.weibo.cn
// @namespace https://github.com/zunsthy
// @include https://weibo.com/*
// @include https://m.weibo.cn/*
// @match https://weibo.com/*
// @match https://m.weibo.cn/*
// @grant none
// ==/UserScript==
((utils) => {
const reUrlStr = /.+?(?=(?:.{4})+$|$)/g
const reMID = /.+?(?=(?:.{7})+$|$)/g
const mUrlHost = 'm.weibo.cn'
const webUrlHost = 'weibo.com'
const reCheckMID = /^\d{15,}/
const map = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
const len = map.length
const pInt = (s) => parseInt(s, 10)
const tInt = (n) => n.toString(10)
const wbStringToID = (s) => tInt(s.split('').reduce((r, c) => r * len + map.indexOf(c), 0))
const wbIDToString = (i) => {
let s = ''
for (let n = pInt(i); n > 0; r = n % len, s = map[r] + s, n = (n - r) / len);
return s
}
const toMID = (wid) => {
let id = ''
for (let r, i; r = reUrlStr.exec(wid, reUrlStr.lastIndex); id += id ? i.padStart(7, '0') : i)
i = wbStringToID(r[0])
return id
}
const toUrlStr = (mid) => {
let str = ''
for (let r, s; r = reMID.exec(mid, reMID.lastIndex); str += s)
s = wbIDToString(r[0])
return str
}
const trans = (u) => {
const url = new URL(u)
const pieces = url.pathname.split('/')
if (!pieces[2]) reutrn
if (mUrlHost === url.hostname || reCheckMID.test(pieces[2])) return toUrlStr(pieces[2])
if (webUrlHost === url.hostname) return toMID(pieces[2])
}
const getShareUrl = (u) => {
const url = new URL(u)
const pieces = url.pathname.split('/')
const mid = reCheckMID.test(pieces[2]) ? pieces[2] : toMID(pieces[2])
const uid = mUrlHost === url.hostname ? document.querySelector('a.m-img-box').href.match(/\d+/)[0] : pieces[1]
return `https://${webUrlHost}/${uid}/${mid}`
}
utils.trans = (u = window.location) => trans(u)
utils.getShareUrl = (u = window.location) => getShareUrl(u)
})(window._WBUtils = window._WBUtils || {})