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
|
# File 'lib/print-scorm.rb', line 21
def run(zipfile, target_name)
@@browser = Watir::Browser.new :firefox unless @@browser
zipfile = File.join(Dir.pwd, zipfile) unless zipfile.start_with? "/"
directory = File.dirname(zipfile)
if @skip_existing && FileTest.exists?(target_name)
puts "Skipping the already existing `#{target_name}`"
return nil
end
tempdir = File.join(directory, "__tmp")
p tempdir
`rm -r "#{tempdir}"` if FileTest.exist? tempdir
`mkdir -p "#{tempdir}"`
`unzip "#{zipfile}" -d "#{tempdir}"`
b = @@browser
b.window.resize_to 1200, 900
b.goto "file://"+tempdir+"/scorm2004RLO.htm"
b.alert.ok
if b.html.include? "This project does not contain any pages."
return nil
end
begin
b.button(title: "Full Screen").click
rescue
puts "No fullscreen button!!"
end
sleep 1
all_screenshots = []
page_index = 0
screenshot(tempdir, page_index, b, all_screenshots)
while b.button(id: "x_nextBtn").enabled?
b.button(id: "x_nextBtn").click sleep MAX_SLEEPTIME
start_time = Time.now
while !all_bullets_loaded(b) && (Time.now - start_time < MAX_TIMEOUT)
sleep 1
end
page_index += 1
last_screenshot = screenshot(tempdir, page_index, b, all_screenshots)
skip_click = false
while b.button(id: "nextBtn").exists? && b.button(id: "nextBtn").enabled?
unless skip_click
b.button(id: "nextBtn").click else
skip_click = false
end
sleep MAX_SLEEPTIME
page_index += 1
new_screenshot = screenshot(tempdir, page_index, b, all_screenshots)
if new_screenshot[:hash] == last_screenshot[:hash]
puts "Loop detected. Avoiding..."
page_index -= 1
all_screenshots.pop
break
else
last_screenshot = new_screenshot
end
end
accordion_id = 1
accordion_button = b.h3(id: ACCORDION_ID + accordion_id.to_s)
while accordion_button.exists?
accordion_button.click
sleep MAX_SLEEPTIME
page_index += 1
new_screenshot = screenshot(tempdir, page_index, b, all_screenshots)
accordion_id += 1
accordion_button = b.h3(id: ACCORDION_ID + accordion_id.to_s)
end
oldScroll = @@browser.execute_script("return document.getElementById('x_pageHolder').scrollTop")
i = 1
loop do
b.execute_script "document.getElementById('x_pageHolder').scrollTo(0, document.getElementById('x_pageHolder').clientHeight*#{i})"
newScroll = @@browser.execute_script("return document.getElementById('x_pageHolder').scrollTop")
break if oldScroll == newScroll
sleep 0.5
page_index += 1
screenshot(tempdir, page_index, b, all_screenshots)
oldScroll = newScroll
i += 1
end
go_ahead_buttons = b.buttons(class: "ui-button-text-only")
if go_ahead_buttons.size > 0
if b.html.include? "<h2 aria-live=\"assertive\">Sommario</h2>"
go_ahead_buttons[0].click
skip_click = true
end
end
end
`convert #{all_screenshots.join(" ")} "#{target_name}"`
`rm -r "#{tempdir}"`
end
|