-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
95 lines (87 loc) · 3.67 KB
/
popup.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
let currentHighlight = null;
function addDebugMessage(message) {
const debugElement = document.getElementById('debug');
debugElement.innerHTML += message + '<br>';
console.log(message);
}
function generateTreeData() {
return new Promise((resolve, reject) => {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
addDebugMessage("Sending message to content script to get DOM structure");
chrome.tabs.sendMessage(tabs[0].id, { action: "getDOMStructure" }, function(response) {
if (response) {
console.log("DOM Structure received:", response);
addDebugMessage("Received DOM structure");
resolve(response);
} else {
addDebugMessage("No response from content script");
reject(new Error("No response from content script"));
}
});
});
});
}
function initializeJsTree() {
addDebugMessage("Initializing jsTree");
generateTreeData().then((treeData) => {
if (treeData) {
addDebugMessage("Tree data received, initializing jsTree");
console.log("Tree Data for jsTree:", JSON.stringify(treeData, null, 2));
$('#jstree').jstree({
'core': {
'data': treeData,
'themes': {
'name': 'default',
'dots': false,
'icons': true
}
},
'plugins': ['types']
}).on('ready.jstree', function() {
addDebugMessage("jsTree is ready!");
}).on('changed.jstree', function (e, data) {
if (data.selected.length) {
const nodeData = data.instance.get_node(data.selected[0]).original;
addDebugMessage(`Node selected, sending highlight message for ${nodeData.selector}`);
currentHighlight = nodeData.selector;
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "highlightElement",
selector: nodeData.selector
});
});
}
});
} else {
addDebugMessage("No tree data found to initialize");
}
}).catch(error => {
addDebugMessage("Error in tree data generation: " + error);
});
}
document.addEventListener('DOMContentLoaded', function() {
addDebugMessage("DOM content loaded");
initializeJsTree();
document.getElementById('copyButton').addEventListener('click', function() {
if (currentHighlight) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "copyText",
selector: currentHighlight
}, function(response) {
if (response && response.text) {
navigator.clipboard.writeText(response.text).then(function() {
addDebugMessage("Text copied to clipboard");
}, function(err) {
addDebugMessage("Failed to copy text: " + err);
});
} else {
addDebugMessage("No text received to copy");
}
});
});
} else {
addDebugMessage("No element selected to copy text from");
}
});
});