-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
132 lines (115 loc) · 3.11 KB
/
Copy pathpreload.js
File metadata and controls
132 lines (115 loc) · 3.11 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function () {
const SETTINGS_ID = 'simpletex-formula/settings';
const DEFAULT_SETTINGS = {
token: '',
model: 'turbo'
};
function hasRubick() {
return typeof rubick !== 'undefined' && rubick;
}
async function readSettings() {
if (!hasRubick() || !rubick.db) {
return readLocalSettings();
}
try {
const doc = await rubick.db.get(SETTINGS_ID);
return Object.assign({}, DEFAULT_SETTINGS, doc.data || {}, { _rev: doc._rev });
} catch (error) {
return readLocalSettings();
}
}
async function saveSettings(settings) {
const next = Object.assign({}, DEFAULT_SETTINGS, settings);
if (!hasRubick() || !rubick.db) {
writeLocalSettings(next);
return next;
}
try {
const oldDoc = await rubick.db.get(SETTINGS_ID);
await rubick.db.put({
_id: SETTINGS_ID,
_rev: oldDoc._rev,
data: next
});
} catch (error) {
await rubick.db.put({
_id: SETTINGS_ID,
data: next
});
}
writeLocalSettings(next);
return next;
}
function readLocalSettings() {
try {
return Object.assign(
{},
DEFAULT_SETTINGS,
JSON.parse(window.localStorage.getItem(SETTINGS_ID) || '{}')
);
} catch (error) {
return Object.assign({}, DEFAULT_SETTINGS);
}
}
function writeLocalSettings(settings) {
try {
window.localStorage.setItem(SETTINGS_ID, JSON.stringify(settings));
} catch (error) {
// localStorage can be unavailable in restricted contexts.
}
}
function notify(message) {
if (hasRubick() && typeof rubick.showNotification === 'function') {
rubick.showNotification(message);
return;
}
console.log(message);
}
function setHeight(height) {
if (hasRubick() && typeof rubick.setExpendHeight === 'function') {
rubick.setExpendHeight(height);
}
}
function onReady(callback) {
if (hasRubick() && typeof rubick.onPluginReady === 'function') {
rubick.onPluginReady(callback);
return;
}
window.addEventListener('DOMContentLoaded', function () {
callback({ code: 'formula', type: 'manual', payload: null });
});
}
function onEnter(callback) {
if (hasRubick() && typeof rubick.onPluginEnter === 'function') {
rubick.onPluginEnter(callback);
}
}
async function copyText(text) {
if (hasRubick() && rubick.clipboard && typeof rubick.clipboard.writeText === 'function') {
rubick.clipboard.writeText(text);
return;
}
if (navigator.clipboard && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text);
return;
}
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
window.rubickFormula = {
readSettings,
saveSettings,
notify,
setHeight,
onReady,
onEnter,
copyText
};
})();