-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
136 lines (129 loc) · 4.69 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* content.js
* (c) Ben Pruitt, 2017 [MIT License, see LICENSE]
*
* js logic that will be injected into digikey pages, provides functionality
* to copy part-related metadata from search / filter pages
*
*/
// (somewhat hacky) means of setting the clipboard contents in chrome
function executeCopy(text) {
// store window coordinates so that we can avoid a scroll event when we
// focus on our injected textarea
var x = window.scrollX, y = window.scrollY;
// create a textarea to populate with the text to copy
var input = document.createElement('textarea');
document.body.appendChild(input);
input.value = text;
// focus on the input element + select so that when we execute the copy
// command our text will be copied
input.focus();
input.select();
// scroll to original viewport location
window.scrollTo(x, y);
document.execCommand('Copy');
// remove the temporary text area
input.remove();
}
// Safe text extraction w/ newline replacement
function getText(el) {
var d = new DOMParser().parseFromString(el.innerHTML, 'text/html');
return d.body.textContent.replace(/\r?\n|\r/g, ' ');
}
// Helper function to get nested child elements based on element
// tags / positions
function getNestedChild(el, child_tags, child_positions) {
for (i = 0; i < child_tags.length; i++) {
el = el.getElementsByTagName(child_tags[i])[child_positions[i]];
}
return el;
}
// Factory function to generate digikey part information getters
function genChildTextGetter(cell_class, child_tags, child_positions){
var _getter = function(el) {
var cell_el = el.getElementsByClassName(cell_class)[0];
return getText(getNestedChild(cell_el, child_tags, child_positions));
}
return _getter;
}
// Getter functions for the various digikey fields
var DIGIKEY_GETTERS = {
'digikey_description': genChildTextGetter('tr-description', [], []),
'manufacturer_name': genChildTextGetter('tr-vendor', ['span', 'a', 'span'],
[0, 0, 0]),
'manufacturer_part_num': function(el, opts) {
var part_num = genChildTextGetter('tr-mfgPartNumber', ['a'], [0])(el);
if (opts.excel_urls) {
var url = DIGIKEY_GETTERS.datasheet_url(el);
var excel_txt = '=HYPERLINK("' + url + '","' + part_num + '")';
return excel_txt;
} else {
return part_num;
}
},
'digikey_part_num': function(el, opts) {
var part_num = genChildTextGetter('tr-dkPartNumber', ['a'], [0])(el);
if (opts.excel_urls) {
var url = DIGIKEY_GETTERS.digikey_url(el);
var excel_txt = '=HYPERLINK("' + url + '","' + part_num + '")';
return excel_txt;
} else {
return part_num;
}
},
'digikey_label': function(){return 'Digikey';},
'digikey_unit_price': genChildTextGetter('tr-unitPrice', [], []),
'digikey_min_qty': genChildTextGetter('tr-minQty', [], []),
'digikey_url': function(el) {
var cell_el = el.getElementsByClassName('tr-dkPartNumber')[0];
var a_el = getNestedChild(cell_el, ['a'], [0]);
return a_el.href;
},
'datasheet_url': function(el) {
var cell_el = el.getElementsByClassName('tr-datasheet')[0];
var a_el = getNestedChild(cell_el, ['a'], [0]);
return a_el.href;
}
}
// Click handler for the injected copy buttons
function handleCopyClick(ev) {
ev.preventDefault();
chrome.storage.sync.get(
['active-fields', 'excel-urls'],
function(res){
var acfs = res['active-fields'];
if (!acfs) {
alert('digikey-clip settings must be saved before copying!');
return;
}
var part_row_el = ev.target.parentNode.parentNode.parentNode;
var clip_text = '';
for (var i = 0; i < acfs.length; i++) {
console.log('[digikey-clip] getting field:', acfs[i]);
var g_func = DIGIKEY_GETTERS[acfs[i]];
if (g_func) {
clip_text += g_func(part_row_el, {excel_urls: res['excel-urls']});
}
clip_text += '\t';
}
executeCopy(clip_text.substring(0, clip_text.length-1));
ev.target.innerHTML = 'copied!';
setTimeout(function() {ev.target.innerHTML='copy'}, 500);
}
)
}
// Find all of the td cells for item part numbers and add the copy button
var part_no_els = document.getElementsByClassName("tr-dkPartNumber");
for (var i=0; i < part_no_els.length; i++) {
var btn_div = document.createElement('div');
var btn = document.createElement('button');
btn_div.appendChild(btn);
btn.innerHTML = 'copy';
btn.onclick = handleCopyClick;
btn_div.setAttribute('style', 'position:"absolute"; right:0; right:0')
btn_div.style.position = 'absolute';
btn_div.style.right = 0;
btn_div.style.top = 0;
part_no_els[i].insertBefore(btn_div, part_no_els[i].firstChild);
part_no_els[i].setAttribute('style', 'position: relative');
}