-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMLElementPathfinder.rb
110 lines (96 loc) · 2.57 KB
/
HTMLElementPathfinder.rb
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
require 'rainbow'
require 'nokogiri'
require 'net/http'
require 'uri'
require 'awesome_print'
require 'json'
module DSC
class HTMLElementPathfinder
attr_reader :html_string, :path
def initialize websites
@websites = websites
@elements_to_ignore = [
'title', 'script'
]
end
def run
@websites.each do |id, data|
STDOUT.puts "\nFind '#{data[:text]}' in '#{data[:url]}':"
if data[:method] == 'POST'
@html_string = open_post(data[:url], data[:params])
else
@html_string = open_get(data[:url])
end
find_path_to_text(data[:text], data[:url])
STDOUT.puts view_threaded_path
end
end
def find_path_to_text text, url
html = Nokogiri::HTML(@html_string)
elements_found = html.xpath("//*[contains(text(), '#{text}')]")
# TODO: MULTIPLES!
if elements_found.empty?
STDOUT.puts "#{text} not found in #{url}"
else
elements_found.each do |element|
if consider_element?(element)
initialize_path(element)
get_parents_until_body_tag(element)
end
end
end
end
def view_threaded_path
if @path.nil?
STDOUT.puts "@Path is empty!"
else
i = -1
threaded_array = @path.reverse.map do |tag|
i += 1
classes = "class=\"#{Rainbow(tag[:attributes]['class']).bright}\""
if tag[:attributes]['id']
id = "id='#{tag[:attributes]['id']}' "
end
"#{" " * i}#{Rainbow(tag[:name]).green} #{Rainbow(id).orange}#{Rainbow(classes).blue}"
end
return "#{threaded_array.join("\n")}"
end
end
private
def initialize_path element
@path = [
{
name: element.name,
attributes: element.attributes
}
]
end
def consider_element? element
if @elements_to_ignore.include?(element.name)
return false
else
return true
end
end
def open_get(url)
Net::HTTP.get(URI.parse(url))
end
def open_post(url, params)
Net::HTTP.post_form(
URI.parse(url),
eval(params)
).body
end
def get_parents_until_body_tag element
@path << {
name: element.parent.name,
attributes: element.parent.attributes
}
unless element.parent.name == "body"
get_parents_until_body_tag(element.parent)
end
end
end
end
@hep = DSC::HTMLElementPathfinder.new(@websites)
@hep.run