forked from dodying/UserJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.user.js
83 lines (83 loc) · 2.57 KB
/
meta.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
72
73
74
75
76
77
78
79
80
81
82
83
/* eslint-env browser */
// ==UserScript==
// @name []meta
// @description
// @version 1.11
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @author dodying
// @namespace https://github.com/dodying/
// @supportURL https://github.com/dodying//UserJs/issues
// @icon https://gitee.com/dodying/userJs/raw/master/Logo.png
// @run-at document-end
// @require https://cdnjs.cloudflare.com/ajax/libs/psl/1.1.31/psl.min.js
// @noframes
// @include *
// @connect *
// ==/UserScript==
(function () {
const $ = (e) => document.querySelector(e);
const $$ = (e) => document.querySelectorAll(e);
const getFav = (url, func = undefined) => {
GM_xmlhttpRequest({
method: 'GET',
timeout: 5000,
url,
responseType: 'blob',
onload: (res) => {
if (res.status === 200 && res.responseHeaders.match('content-type: image') && !!res.response && res.response.size !== 492) {
setIcon();
} else if (func) func();
},
onerror: () => {
if (func) func();
},
ontimeout: () => {
if (func) func();
},
});
};
const setIcon = (f = true) => {
const icon = GM_getValue('icon', {});
icon[window.location.host] = f;
GM_setValue('icon', icon);
};
const canvasFav = (a) => {
const c = document.createElement('canvas');
const r = 8;
c.width = 2 * r;
c.height = 2 * r;
const ctx = c.getContext('2d');
ctx.fillStyle = '#000';
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.font = `bold ${r * 2}px sans-serif`;
ctx.fillText(a, r, r);
return c.toDataURL();
};
const newFav = () => {
const fav = document.createElement('link');
fav.rel = 'shortcut icon';
/* global psl */
fav.href = canvasFav(psl.parse(window.location.host).sld.substr(0, 1));
document.head.appendChild(fav);
setIcon(false);
};
if ($('link[type="application/rss+xml"],link[type="application/atom+xml"]')) {
[...$$('link[type="application/rss+xml"],link[type="application/atom+xml"]')].forEach((i, j) => {
GM_registerMenuCommand(`RSS-${j}:${i.title || ''}`, () => {
window.open(i.href);
});
});
}
const icon = GM_getValue('icon', {});
if (window.location.host in icon) {
if (icon[window.location.host] === false) newFav();
} else if (!$('link[rel*="icon"]')) {
getFav(`${window.location.origin}/favicon.ico`, () => {
getFav(`https://www.google.com/s2/favicons?domain=${window.location.host}`, newFav);
});
}
}());