-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
162 lines (140 loc) · 4.59 KB
/
script.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
150
151
152
153
154
155
156
157
158
159
160
161
162
const input = document.getElementById("url-input");
let state = false;
let isLoading = false;
const displayArea = document.getElementById("display-area");
const loader = document.getElementById("loader-container");
const navs = document.querySelectorAll(".navs");
let URL = ""
const pythonEditor = CodeMirror(document.getElementById("generation-area"), {
value: "from selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\nch = os.getcwd() + '/python/tools/chromedriver'\noptions = Options()\noptions.set_headless(headless=True)\noptions.add_argument('--disable-gpu')\noptions.add_argument('--disable-dev-shm-usage')\noptions.add_argument('--no-sandbox')\ndriver = webdriver.Chrome(options=options, executable_path=ch)",
mode: "python",
lineNumbers: true,
theme: "shadowfox"
});
let currentDisplay = "elements";
let webContent = {
"elements": 0,
"tables": 0,
"links": 0,
"images": 0,
"others": 0,
}
let buttonsText = [{
"div": [
"id='main'"
],
},
{
"p": [
"class='text'",
"This website is made for Test"
],
},
{
"h1": [
"My Website"
],
},
{
"article": [
"id='dummy'"
]
},
];
window.onload = () => {
let empty = document.createElement("span");
empty.innerText = "Enter URL to display elements";
displayArea.appendChild(empty);
displayArea.classList.add("empty");
}
const checkURL = (url) => {
URL = url
var urlPat = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
if (urlPat.test(url) === false) {
document.getElementById("status").style.visibility = "visible";
state = false;
return false;
} else {
document.getElementById("status").style.visibility = "hidden";
state = true;
return true;
}
}
const switchDisplay = (e) => {
document.querySelector(`li[data-name=${currentDisplay}]`).classList.remove("hovered");
let target = null;
if (e.target.parentNode.className === "navs")
target = e.target.parentNode;
else
target = e.target;
currentDisplay = target.getAttribute("data-name");
buttonsText = webContent[currentDisplay];
renderButtons(buttonsText);
target.classList.add("hovered");
}
const submitURL = (e) => {
if (e.keyCode === 13 && state) {
isLoading = true;
loader.style.display = "flex";
var python = require('python-shell');
var options = {
args: [URL, "github"],
mode: 'json'
}
var script = new python.PythonShell('./python/main.py', options);
script.on('message', (message) => {
isLoading = false
loader.style.display = "none";
webContent = message;
buttonsText = webContent["elements"]
renderButtons(buttonsText);
navs[0].classList.add("hovered");
navs.forEach((nav) => {
nav.addEventListener("click", switchDisplay)
nav.childNodes[1].innerText = webContent[nav.getAttribute("data-name")].length
})
})
}
}
input.addEventListener("input", () => checkURL(input.value));
input.addEventListener("keydown", submitURL);
function renderButtons(buttonsText) {
while (displayArea.firstChild)
displayArea.removeChild(displayArea.firstChild);
displayArea.classList.remove("empty");
for (var i = 0; i < buttonsText.length; i++) {
var newButton = document.createElement("div");
newButton.setAttribute("class", "button");
var buttonHead = document.createElement("h2");
buttonHead.setAttribute("class", "button-head");
buttonHead.innerHTML = Object.keys(buttonsText[i]);
newButton.appendChild(buttonHead);
var buttonParaCon = document.createElement("div");
buttonParaCon.setAttribute("class", "button-para-container");
for (var j in buttonsText[i][Object.keys(buttonsText[i])]) {
var buttonPara = document.createElement("p");
buttonPara.setAttribute("class", "button-para");
buttonPara.innerHTML = buttonsText[i][Object.keys(buttonsText[i])][j].slice(0, 35);
buttonParaCon.appendChild(buttonPara);
}
newButton.appendChild(buttonParaCon);
displayArea.appendChild(newButton);
newButton.addEventListener("click", selectButton);
if (isLoading)
loader.style.display = "none";
}
}
function selectButton(e) {
var target = null;
if (e.target.parentNode.className === "button" || e.target.parentNode.className === "button selected") {
target = e.target.parentNode;
} else if (e.target.parentNode.parentNode.className === "button" || e.target.parentNode.parentNode.className === "button selected") {
target = e.target.parentNode.parentNode;
} else if (e.target.className === "button" || e.target.className === "button selected") {
target = e.target;
}
var x = target.innerText.split('\n')[1];
var p = x.split("=")[1];
var text = "\ndriver.find_element_by_xpath('" + p + "')";
pythonEditor.replaceRange(text, CodeMirror.Pos(pythonEditor.lastLine()));
}