-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
58 lines (50 loc) · 1.74 KB
/
contentScript.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
// Helper functions
function getElementsByClassName(node, classname) {
if (node.getElementsByClassName) { // use native implementation if available
return node.getElementsByClassName(classname);
} else {
return (function getElementsByClass(searchClass, node) {
if (node == null)
node = document;
var classElements = [],
els = node.getElementsByTagName("*"),
elsLen = els.length,
pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)"),
i, j;
for (i = 0, j = 0; i < elsLen; i++) {
if (pattern.test(els[i].className)) {
classElements[j] = els[i];
j++;
}
}
return classElements;
})(classname, node);
}
}
function copyToClipboard(text) {
var copyDiv = document.createElement('div');
copyDiv.contentEditable = true;
document.body.appendChild(copyDiv);
copyDiv.innerHTML = text;
copyDiv.unselectable = "off";
copyDiv.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyDiv);
return true;
}
// End helper functions
// Main
function main() {
var title = getElementsByClassName(document, 'card-detail-title-assist')[0].innerText;
var prefix = title.match(/s\d+_c\d+/i);
var hasPrefix = prefix != undefined;
if (hasPrefix) {
var branch = 'BCH_' + prefix[0];
console.log('Get Branch: "' + branch + '" copiado!');
chrome.runtime.sendMessage({status: true});
return copyToClipboard(branch);
}
return alert('Este cartão não tem um prefixo S000_C000 válido!');
}
main();