-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
127 lines (92 loc) · 3.19 KB
/
extension.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
const vscode = require('vscode');
const fetch = require("node-fetch");
const https = require('https');
/*
I'm forced to use "rejectUnauthorized" because latest electron version
shipped with VS Code (as 26th of February 2022) is still using node 14, and suffer
of an hardcoded root certificate issue for let's encrypt
This is the workaround suggested by Microsoft:
https://github.com/microsoft/vscode/issues/136787#issuecomment-969065291
*/
https.globalAgent.options.rejectUnauthorized = false;
var myStatusBarItem;
var hideStatusBar;
// Used to retain a cache instead of calling the web API often
var cachedData;
var cacheTtl = 10920; // Expressed in ms. 10920 ~ slightly more than 3hrs
// Strings used for status bar
const BAR_COLLAPSED = `$(ruby) Click for Comfucius`;
const BAR_LOADING = `$(ruby) Loading... `;
const BAR_ERROR = `$(ruby) No quotes ಥ_ಥ `;
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// -9999 will set a very low priority for status bar ordering, thus making sure
// I will be always the "left-anchored" rightmost component
myStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, -9999);
const toggleVis = 'comfucius.toggleVisibility';
context.subscriptions.push(vscode.commands.registerCommand(toggleVis, () => {
hideStatusBar = !hideStatusBar;
if (hideStatusBar)
myStatusBarItem.text = BAR_COLLAPSED;
else
myStatusBarItem.text = BAR_LOADING;
}));
myStatusBarItem.command = toggleVis;
context.subscriptions.push(myStatusBarItem);
myStatusBarItem.show();
refreshPhrase();
}
function deactivate() {}
function refreshPhrase(){
if(cachedData){
// Pass cloned object to avoid cache clearing while parsing the phrase
showPharse({...cachedData});
}
else{
// @ts-ignore
fetch('https://comfucius.xyz/quotes/api/get-fake-quote')
.then(response => response.json())
.then(dataFromApi => {
// Save inside cache data I've retrieved from API...
cachedData = dataFromApi;
// ...but also dischange it after a certain amount of time
setTimeout(()=>{ cachedData = null }, cacheTtl * 1000);
showPharse(cachedData);
});
}
}
async function showPharse(data){
if (data != null){
// Build the fake quote from the input data
let fakeQuote = `${data.Phrase} ~ ${data.Thinker}`;
if (hideStatusBar)
myStatusBarItem.text = BAR_COLLAPSED;
else
myStatusBarItem.text = `$(megaphone) ${fakeQuote}`;
var textLength = fakeQuote.length;
await new Promise(r => setTimeout(r, vscode.workspace.getConfiguration().get('comfucius.startScrollDelay') * 1000));
while (textLength > 0){
textLength--;
fakeQuote = fakeQuote.substring(1);
if (hideStatusBar)
myStatusBarItem.text = BAR_COLLAPSED;
else
myStatusBarItem.text = `$(megaphone) ${fakeQuote}`;
await new Promise(r => setTimeout(r, vscode.workspace.getConfiguration().get('comfucius.scrollPerCharacter') * 1000));
}
}
else{
if (hideStatusBar)
myStatusBarItem.text = BAR_COLLAPSED;
else
myStatusBarItem.text = BAR_ERROR;
await new Promise(r => setTimeout(r, 10000));
}
setTimeout(refreshPhrase, vscode.workspace.getConfiguration().get('comfucius.timeDelay') * 1000);
}
module.exports = {
activate,
deactivate
}