-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPagesSource.js
72 lines (54 loc) · 1.98 KB
/
getPagesSource.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
// @author Rob W <http://stackoverflow.com/users/938089/rob-w>
// Demo: var serialized_html = DOMtoString(document);
function DOMtoString(document_root) {
var html = '';
var node = document_root.firstChild;
var item = document_root.getElementsByClassName("ic-truncate item-clickable");
while (node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
html += node.outerHTML;
break;
case Node.TEXT_NODE:
html += node.nodeValue;
break;
case Node.CDATA_SECTION_NODE:
html += '<![CDATA[' + node.nodeValue + ']]>';
break;
case Node.COMMENT_NODE:
html += '<!--' + node.nodeValue + '-->';
break;
case Node.DOCUMENT_TYPE_NODE:
// (X)HTML documents are identified by public identifiers
html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
break;
}
node = node.nextSibling;
}
var num_matches = html.match(/div class="ic-truncate item-clickable/gi).length;
var items='';
var pos = 0
var pos2 = 0
var index = 'div class="ic-truncate item-clickable'
var indexEnd = 'div'
var array = [];
for (i = 0; i < num_matches; i++) {
pos = html.indexOf(index)+index.length
html = html.substring(pos,html.length)
pos2 = html.indexOf(indexEnd)
content = html.substring(0,pos2)
items+= content.substring(2, content.length-2)+ '\n'
array.push(content.substring(2, content.length-2))
posEnd = pos2 +4
html = html.substring(posEnd,html.length)
}
// d3.csv("baseFoodPrint.csv", function(data) {
// console.log(data[0]);
// });
return items;
// return html;
}
chrome.runtime.sendMessage({
action: "getSource",
source: DOMtoString(document)
});