/
activate-hover.js
149 lines (128 loc) · 4.29 KB
/
activate-hover.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
137
138
139
140
141
142
143
144
145
146
147
148
149
// This is included and executed in the inspected page
// function inserted() {
// console.log('External script attached');
// }
// inserted();
var $ = require('jquery');
// console.log("activate-hover.js has been injected")
window.addEventListener('message', (message) => {
// console.log("received message in injected script");
if (message.data.name === "updateSelectionType" && message.data.message) {
hoverSelection(message.data.message);
} else if (message.data.name === "updateHighlight" && message.data.message) {
console.log("received in activate hover");
toggleHighlight(message.data.message);
}
})
// Toggle highlight
function toggleHighlight(state) {
window.highlight = (state === "on") ? true : false;
}
// initialize as hover
hoverSelection("hover");
// initialize highlight state
window.highlight = true;
function hoverSelection(selType) {
// BIND HOVER FUNCTIONS
let svgGraphicsElements = ["circle", "ellipse", "image", "line", "path", "polygon", "polyline", "rect", "text", "use"]
if (selType === "hover") {
// clear other selection types from elements and bind hover to elements
svgGraphicsElements.forEach((element, i) => {
$(element).unbind("click.d3Ext");
$(element).bind("mouseenter.d3Ext", function (event) { updateConsole($(this), event); });
$(element).bind("mouseleave.d3Ext", function () { updateConsoleOut($(this)) });
});
} else if (selType === "click") {
svgGraphicsElements.forEach((element, i) => {
$(element).unbind("mouseenter.d3Ext");
$(element).unbind("mouseleave.d3Ext");
$(element).bind("click.d3Ext", function (event) { updateConsole($(this), event); });
});
} else { // selType === "off"
svgGraphicsElements.forEach((element, i) => {
$(element).unbind("click.d3Ext");
$(element).unbind("mouseenter.d3Ext");
$(element).unbind("mouseleave.d3Ext");
});
}
}
function updateConsole(selection) {
//highlight selection
if (window.highlight) {
$('*').removeClass("d3-debugger-hovered-svg");
selection.addClass("d3-debugger-hovered-svg");
} else {
$('*').removeClass("d3-debugger-hovered-svg");
}
var tagJSON = getHTMLTag(selection);
var attributesJSON = getHTMLAttributes(selection);
var ancestryJSON = getAncestry(selection);
var dataJSON = getData(selection);
var deconData = {
tag: JSON.stringify(tagJSON),
attributes: JSON.stringify(attributesJSON),
ancestry: JSON.stringify(ancestryJSON),
data: JSON.stringify(dataJSON)
};
// console.log(deconData);
window.postMessage({
type: "FROM_PAGE_TO_CONTENT_SCRIPT",
message: deconData,
}, "*");
// console.log("sent message");
}
function updateConsoleOut(selection) {
selection.removeClass("d3-debugger-hovered-svg");
}
////////////////// get panel properties /////////////////
function getData(selection) {
return selection[0].__data__;
}
function getHTMLTag(selection) {
var tagJSON = {};
tagJSON["tag"] = selection[0].tagName
return tagJSON
}
function getHTMLAttributes(selection) {
attributeNames = [];
let attributesJSON = {};
$.each(selection[0].attributes, function (i, attr) {
if (attr.specified) {
attrValueWithoutCustomClass = attr.value.replace("d3-debugger-hovered-svg", "");
if (attrValueWithoutCustomClass.length > 0) {
attributesJSON[attr.name] = attrValueWithoutCustomClass;
attributeNames.push(attr.name);
}
}
});
let sortedAttributeNames = attributeNames.sort();
let sortedAttributesJSON = {};
sortedAttributeNames.forEach((element, i) => {
sortedAttributesJSON[element] = attributesJSON[element];
})
return sortedAttributesJSON;
}
function getAncestry(selection) {
$("#console-ancestry").html(""); //clear
let ancestry = selection.parents().toArray().reverse(); //parents
ancestry.push(selection[0])
let ancestryJSON = {} // maps indices to string of HTML per ancestor
ancestry.forEach((ancestor, i) => {
let parentHTMLCode = generateHTMLCode(ancestor)
ancestryJSON[i] = parentHTMLCode;
})
return ancestryJSON;
}
function generateHTMLCode(selection) {
var resultJSON = {
main: selection.tagName.toLowerCase(),
attributes: []
}
$(selection.attributes).each(function () {
nodeValueWithoutCustomClass = this.nodeValue.replace("d3-debugger-hovered-svg", "");
if (nodeValueWithoutCustomClass.length > 0) {
resultJSON.attributes.push(this.nodeName + "=\"" + nodeValueWithoutCustomClass + "\"");
}
});
return resultJSON;
}