forked from dimdenGD/YeahTwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathus.js
184 lines (155 loc) · 5 KB
/
us.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// ==UserScript==
// @name Yeah! for Twitter
// @namespace http://tampermonkey.net/
// @version ${manifest.version}
// @description Adds Yeah! button to Twitter, essentially a public Like
// @author dimden.dev
// @match https://x.com/*
// @match https://twitter.com/*
// @icon https://dimden.dev/images/yeah_logo.png
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// ==/UserScript==
// fetch polyfill
function GM_fetch(url, options = {}) {
return new Promise((resolve, reject) => {
const method = options.method || 'GET';
const headers = options.headers || {};
const body = options.body || null;
GM_xmlhttpRequest({
method,
url,
headers,
data: body,
onload(response) {
const responseBody = response.responseText;
const status = response.status;
const statusText = response.statusText;
const responseHeaders = parseHeaders(response.responseHeaders);
resolve(new Response(responseBody, {
status,
statusText,
headers: responseHeaders
}));
},
onerror(error) {
reject(new Error('Network request failed'));
},
ontimeout() {
reject(new Error('Network request timed out'));
}
});
});
}
function parseHeaders(headersString) {
const headers = new Headers();
const lines = headersString.trim().split(/[\r\n]+/);
lines.forEach(line => {
const parts = line.split(': ');
const header = parts.shift();
const value = parts.join(': ');
headers.append(header, value);
});
return headers;
}
class Response {
constructor(body, options) {
this.body = body;
this.status = options.status;
this.statusText = options.statusText;
this.headers = options.headers;
}
text() {
return Promise.resolve(this.body);
}
json() {
return Promise.resolve(JSON.parse(this.body));
}
}
// chrome.storage.local polyfill
window.chrome = window.chrome || {};
chrome.runtime = chrome.runtime || {id: 'userscript'};
chrome.storage = chrome.storage || {};
chrome.storage.local = {
storageKey: 'chromeStorage',
_getStorageObject: function () {
const storage = localStorage.getItem(this.storageKey);
return storage ? JSON.parse(storage) : {};
},
_setStorageObject: function (obj) {
localStorage.setItem(this.storageKey, JSON.stringify(obj));
},
get: function (keys, callback) {
const storageObj = this._getStorageObject();
const result = {};
if (typeof keys === 'string') {
result[keys] = storageObj[keys];
} else if (Array.isArray(keys)) {
keys.forEach(key => {
result[key] = storageObj[key];
});
} else if (typeof keys === 'object') {
Object.keys(keys).forEach(key => {
result[key] = storageObj[key] !== undefined ? storageObj[key] : keys[key];
});
} else {
Object.assign(result, storageObj);
}
callback(result);
},
set: function (items, callback) {
const storageObj = this._getStorageObject();
Object.keys(items).forEach(key => {
storageObj[key] = items[key];
});
this._setStorageObject(storageObj);
if (callback) callback();
},
remove: function (keys, callback) {
const storageObj = this._getStorageObject();
if (typeof keys === 'string') {
delete storageObj[keys];
} else if (Array.isArray(keys)) {
keys.forEach(key => {
delete storageObj[key];
});
}
this._setStorageObject(storageObj);
if (callback) callback();
},
clear: function (callback) {
localStorage.removeItem(this.storageKey);
if (callback) callback();
}
};
if(this.GM_registerMenuCommand) {
GM_registerMenuCommand("Don't like tweet on Yeah", function () {
chrome.storage.local.set({
settings: {
dontLike: true
}
});
});
GM_registerMenuCommand("Like tweet on Yeah", function () {
chrome.storage.local.set({
settings: {
dontLike: false
}
});
});
GM_registerMenuCommand("Clear account tokens", function () {
chrome.storage.local.remove(['yeahToken', 'yeahTokens']);
});
GM_registerMenuCommand("Reset popup settings", function () {
chrome.storage.local.remove(['ignorePopup']);
});
}
let fontstyle = document.createElement('style');
fontstyle.innerHTML = `
@font-face {
font-family: 'RosettaIcons2';
src: url(data:application/x-font-woff;charset=utf-8;base64,{{font_url}});
}
`;
document.head.appendChild(fontstyle);
let YEAH_images = {};