-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcbutils.js
More file actions
95 lines (88 loc) · 2.66 KB
/
Copy pathcbutils.js
File metadata and controls
95 lines (88 loc) · 2.66 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
function cbGetSelectedText(d) {
var S='';
if (d.all) {
S=d.selection.createRange().text;
} else {
S=d.getSelection().toString();
}
return S;
}
async function cbsha1(str) {
var buffer = new TextEncoder('utf-8').encode(str);
const hash = await crypto.subtle.digest('SHA-1', buffer);
return cbHash2hex(hash);
}
function cbHash2hex(buffer) {
var hexCodes = [];
var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 4) {
// Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
var value = view.getUint32(i)
// toString(16) will give the hex representation of the number without padding
var stringValue = value.toString(16)
// We use concatenation and slice for padding
var padding = '00000000'
var paddedValue = (padding + stringValue).slice(-padding.length)
hexCodes.push(paddedValue);
}
// Join all the hex strings into one
return hexCodes.join('');
}
function cbFillPicklist(picklist, options) {
var select = document.getElementById(picklist);
for (var i = 0; i < options.length; i++) {
var el = document.createElement('option');
el.text = options[i].label;
el.value = options[i].value;
select.appendChild(el);
}
}
function cbi18nHTML() {
document.querySelectorAll('[data-locale]').forEach(elem => {
let contentAction = elem.dataset.locale.substring(0, 1);
let titleAction = elem.dataset.locale.substring(1, 1);
let i18nkey = elem.dataset.locale.substring(3);
let i18n = i18nkey ? chrome.i18n.getMessage(i18nkey) : '';
switch (contentAction) {
case 'I': // ignore
break;
case 'S': // substitute
elem.innerHTML = elem.innerHTML.replace(i18nkey, i18n);
break;
default:
case 'T': // translate and set context
elem.innerHTML = i18n;
break;
}
switch (titleAction) {
case 'I': // ignore
break;
case 'S': // substitute
elem.title = elem.title.replace(i18nkey, i18n);
break;
default:
case 'T': // translate and set title
elem.title = i18n;
break;
}
elem.removeAttribute('data-locale');
});
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === 'open_corebos_new_tab' ) {
chrome.storage.sync.get('corebosurl', ({ corebosurl }) => {
console.log(corebosurl);
chrome.tabs.create({'url': corebosurl});
});
}
}
);
function cbCustomEventDispatcher(elEvent, elId){
var event = new Event(elEvent);
document.getElementById(elId).dispatchEvent(event);
}
function cbFormActionUrl() {
let url = document.getElementById('sendto').value + '/index.php?action=EditView&module=' + document.getElementById('convertto').value;
document.getElementById('sendtocbform').action = url;
}