-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
114 lines (91 loc) · 2.91 KB
/
content.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
const debug = true;
const CR = chrome.runtime;
console.log('START CONTENT ON '+window.location.href, window.location)
const ddePageInfo = (() => {
const doc = document.documentElement;
let attr = null;
if(doc.hasAttribute('dde-version'))
attr = doc.getAttribute('dde-version');
if(attr) {
attr = attr.split(' ')
const mode = attr[0]
const version = attr[1]
if(!['strict', 'respectuous', 'broken'].includes(mode)) {
console.error('unknown dde mode ', mode)
status = 'unknown'
}
else if(['0.1'].includes(version))
status = 'enabled'
else
status = 'unknown'
let reason = false;
if(doc.hasAttribute('reason'))
reason = doc.getAttribute('reason');
else if(doc.hasAttribute('data-reason'))
reason = doc.getAttribute('data-reason');
if(status == 'enabled') {
const updateDeclaration = mode + ' ' + version + ' native';
if(doc.hasAttribute('dde-version'))
doc.setAttribute('dde-version', updateDeclaration);
}
return {mode, reason, version, status}
}
else
return false;
})();
if(ddePageInfo && ddePageInfo.status == 'enabled') {
CR.sendMessage({ action: 'loadImplementation', version: ddePageInfo.version }, (response) => {
// Réponse facultative du script de fond
if(debug)
console.log('loadImplementation', ddePageInfo.version, response);
for(s of response.styles) {
console.log(s)
}
});
var stopFirstDOMContentLoaded = true;
window.addEventListener("DOMContentLoaded", (event) => {
if(stopFirstDOMContentLoaded) {
event.stopImmediatePropagation();
stopFirstDOMContentLoaded = false;
if(debug)
console.log("DOMContentLoaded 1* stopped propagation");
}
else
if(debug)
console.log("DOMContentLoaded 2* restore propagation");
},true);
}
const updateJSState = (state) => {
const url = window.location.origin + '/*';
CR.sendMessage({action : 'updateJSState', JSState : state, url}, (resp) => {
console.log('RESP', resp)
if(resp.reload)
window.location.reload();
});
}
CR.sendMessage({action : 'getJSState'}, (response) => {
console.log(response)
if(!response.JSState) {
if(ddePageInfo.status === 'unknown') {
alert("L'implémentation DDE demandée par cette page est inconnue. Votre extension est-t-elle à jour ?")
}
if(ddePageInfo.mode === 'broken') {
const reason = ddePageInfo.reason || "Raison Inconnue"
//alert('Cette page ne fonctionnera hélas pas correctement sans Javascript. Motif : ' + reason)
if(!response.JSConfig) {
dialog('Javascript est requis sur cette page.<br /> Motif : ' + reason+'<br /> Autoriser Javascript sur '+window.location.origin+' ?', {
'Jamais' : () => {updateJSState('never')},
'Toujours' : () => {updateJSState('always')},
'Une fois' : () => {updateJSState('once')}
})
}
}
}
})
/* Update Popup */
CR.onMessage.addListener(function(request, sender, sendResponse) {
console.log(request, sender, sendResponse);
if(request.action === 'getDDEPageInfo') {
sendResponse(ddePageInfo)
}
});