Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
184 lines (162 loc) · 7.26 KB
/
Copy pathbackground.js
File metadata and controls
184 lines (162 loc) · 7.26 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
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
function onTabLoaded(tabId) { //Funzione che attende che la tab aperta abbia finito di caricare
return new Promise(resolve => {
chrome.tabs.onUpdated.addListener(function onUpdated(id, change) {
if (id === tabId && change.status === 'complete') {
chrome.tabs.onUpdated.removeListener(onUpdated);
resolve();
}
});
});
}
chrome.runtime.onMessage.addListener(
async function (request, sender, sendResponse) {
if (request.action == "openExternalJsonEditor") { //Apertura di json editor su nuova pagina
const tab = await chrome.tabs.create({ url: chrome.runtime.getURL('website/jsonEditor.html') });
//Apriamo la nuova tab, aspettiamo che sia pronta e poi inviamo i dati.
//Sfortunatamente il caricamento degli editor è asincrono quindi anche se la tab è pronta non è detto che lo sia il listener
//aggiungiamo un timeout e se va in errore tentiamo di nuovo. Non è una buona soluzione lo so (potremmo gestirla inversa o usare altre API)
await onTabLoaded(tab.id);
await new Promise(r => setTimeout(r, 100));
try {
await chrome.tabs.sendMessage(tab.id, {
action: 'setData',
data: request.data,
});
} catch (e) {
console.log('Messaggio non ricevuto, riprovo');
await new Promise(r => setTimeout(r, 400));
await chrome.tabs.sendMessage(tab.id, {
action: 'setData',
data: request.data,
});
}
} else if (request.action == 'changeExtensionIcon') { //Cambiamo l'icona dell'estensione
if (request.data == 'light') {
chrome.action.setIcon({
path: {
"48": "/assets/icons/48dark.png",
"128": "/assets/icons/128dark.png"
}
});
} else {
chrome.action.setIcon({
path: {
"48": "/assets/icons/48.png",
"128": "/assets/icons/128.png"
}
});
}
} else if ('notify') {
chrome.notifications.create(
'updateNotify',
{
type: 'basic',
iconUrl: '/assets/icons/48.png',
title: 'Aggiornamento disponibile',
message: 'Ciao, è disponibile una nuova versione di DarkPlatform. Premimi per aggiornarmi'
}
)
}
return true;
});
/**Verifica configurazioni
* Il seguente codice si occupa di controllare se sono presenti le configurazioni di default dell'estensione
* In questo modo possiamo specificare i comportamenti di default quando l'utente ha installato l'estensione e non ancora specificato le proprie preferenze
*/
chrome.storage.sync.get(null, (result) => {
debugger;
//console.log(JSON.stringify(result));
chrome.storage.sync.set(
{
'hints': (result.hints === true || result.hints === false ) ? result.hints : true,
'platBot': ( result.platBot === true || result.platBot === false ) ? result.platBot : true,
'editorTema': result.editorTema ? result.editorTema : "aceTomorrowNight",
'fontSizeEditor': result.fontSizeEditor ? result.fontSizeEditor : "12",
'macroCommentoBloccoEditor': result.macroCommentoBloccoEditor ? result.macroCommentoBloccoEditor : false,
'aceBeautifier': result.aceBeautifier ? result.aceBeautifier : false,
'notificheAbilitate': (result.notificheAbilitate === true || result.notificheAbilitate === false) ? result.notificheAbilitate : true,
'notificheGiorniReminder': result.notificheGiorniReminder ? result.notificheGiorniReminder : "7",
"firstKeyJsonEditor": result.firstKeyJsonEditor ? result.firstKeyJsonEditor : JSON.stringify({
"key": "Control",
"keyCode": 17,
"which": 17,
"code": "ControlLeft"
}),
"secondKeyJsonEditor": result.secondKeyJsonEditor ? result.secondKeyJsonEditor : JSON.stringify({
"key": "q",
"keyCode": 81,
"which": 81,
"code": "KeyQ"
}),
'gaeSync': (result.gaeSync === true || result.hints === false ) ? result.gaeSync : true,
'disableSwipeToGoBack': (result.disableSwipeToGoBack === true || result.disableSwipeToGoBack === false) ? result.disableSwipeToGoBack : true,
'dpToVS': (result.dpToVS === true || result.dpToVS === false) ? result.dpToVS : false,
'vsToDpPort': result.vsToDpPort ? result.vsToDpPort : 3005,
}, function () { });
});
//Codice che apre il changelog quando viene aggiornata l'estensione
chrome.storage.local.get(['latestChangeLogShown'], (result) => {
//console.log(JSON.stringify(result));
console.log(result);
result.latestChangeLogShown = result.latestChangeLogShown ? result.latestChangeLogShown : '1.0.0';
if (chrome.runtime.getManifest().version > result.latestChangeLogShown) {
chrome.tabs.create({ url: chrome.runtime.getURL('website/guida.html') });
chrome.storage.local.set(
{
'latestChangeLogShown': chrome.runtime.getManifest().version
}, function () { });
}
});
//Se l'utente preme sulla notifica viene riportato a github
chrome.notifications.onClicked.addListener(
function (id) {
if (id == 'updateNotify') {
chrome.tabs.create({ url: 'https://github.com/LODYZ/darkPlatform' });
}
}
)
//Verifico le chiamate http che vengono effettuate al fine di capire se l'utente ha allineato o meno le azioni gae
chrome.storage.sync.get(['gaeSync'], (result) => {
if(result.gaeSync){
//Consinte nel tenere il service worker sempre attivo
const keepAlive = () => {setInterval(chrome.runtime.getPlatformInfo, 20e3); console.log('Service worker acceso')};
chrome.runtime.onStartup.addListener(keepAlive);
keepAlive();
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
console.log('Debug - GAE STATUS. details passato: '+JSON.stringify(details));
if (details.method=='PUT' && details.url.includes('getActions/detail?') && new URLSearchParams(details.url).get('actionId')) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(details.tabId, { action: "updateGaeStatus", status:'NOT_SYNC', actionId: new URLSearchParams(details.url).get('actionId') });
});
}else if(details.method=='GET' && details.url.includes('executeJs/syncAction?') && new URLSearchParams(details.url).get('actionId') ){
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(details.tabId, { action: "updateGaeStatus", status:'SYNC', actionId: new URLSearchParams(details.url).get('actionId') });
});
}
},
{ urls: ["*://*/*/getActions/detail?*","*://*/*/executeJs/syncAction?*"] },
[]
);
}
});
//Gestione injection css per disabilitare gesture swipe to go back
chrome.storage.sync.get(['disableSwipeToGoBack'], (result) => {
if (result.disableSwipeToGoBack) {
const disableSwipeToGoBack = (tabId, changeInfo, tab) => {
if(!tab || !changeInfo || changeInfo.status !== 'complete' ){
return ;
}
const tabUrl = tab.url;
if (tabUrl.includes("main_designer.jsp?")) {
chrome.scripting.insertCSS({
css: "html, body {overscroll-behavior-x: none;}",
target: { tabId: tabId }
})
}
};
chrome.tabs.onUpdated.addListener(disableSwipeToGoBack);
chrome.tabs.onCreated.addListener(disableSwipeToGoBack);
}
}
)