-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·153 lines (136 loc) · 4.33 KB
/
app.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
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
#! /usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'sinatra'
gem 'puma'
gem 'faker'
end
# exit without Zoho chat widget code
widget_code = ENV.fetch('WIDGETCODE', '').strip
if widget_code == ''
puts <<~HELP
Turbo Zoho v1.0
ur5us
USAGE:
WIDGETCODE=… ./app.rb
WIDGETCODE=… ruby app.rb
HELP
exit 1
end
require 'sinatra/base'
quotes = Hash.new { |h, k| h[k] = Faker::Books::Dune.quote }
ZOHO_JAVASCRIPT = <<~JS
!window.cached_elements && (window.cached_elements = undefined);
document.addEventListener("turbo:before-cache", function() {
if(!cached_elements) {
cached_elements = document.querySelectorAll('[data-turbo-permanent]');
//document.getElementById('cache_script').setAttribute('data-turbo-eval', false)
}
})
document.addEventListener("turbo:load", function() {
let len = cached_elements ? cached_elements.length : 0;
for(let i = 0; i < len; i++) {
document.body.appendChild(cached_elements[i]);
}
cached_elements && window.$ZSIQChat && window.$ZSIQChat.init()
})
var $zoho = $zoho || {};
$zoho.salesiq = $zoho.salesiq || {
widgetcode: "#{widget_code}",
values: {},
ready: function(){
console.log("ready");
$zoho.salesiq.tracking.forcesecure(true);
}
};
var d = document;
var s = d.createElement("script");
s.type = "text/javascript";
s.id = "zsiqscript";
s.defer = true;
s.src = "https://salesiq.zoho.com/widget";
var t = d.getElementsByTagName("script")[0];
t.parentNode.insertBefore(s,t);
$zoho.salesiq.onload = function() {
console.log("onload");
var elemobj = document.querySelectorAll('[data-id="zsalesiq"]');
var length = elemobj.length;
for(var i = 0; i < length; i++){
elemobj[i].setAttribute('data-turbo-permanent','');
}
document.querySelector('.siqembed').removeAttribute('data-turbo-permanent');
}
JS
app = Sinatra.new do
configure do
set :port, 3000
end
template :layout do
# technically the following is ERB but using `HTML` enables syntax highlighting
<<~HTML
<!DOCTYPE html>
<html>
<head>
<link
href="data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAZiuYA////AJ4Z5gD1hSIA5hm2ABkx5gAi2fUAWff/ABl55gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZmZmZmZmZmZmYRERERFmZhERMzETMRERMzMzMzMzMzMzMzMRETMzMzERMzMzMzMzMzMzMzMzERMzMzMzMzMzM1VVd3d3d1VVAFV3d3d3VQAAAAd3d3AAACIAAHd3iIgiIiIgAAACIiJEIiIiIiIiRERERCIiREREREREREREREQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
rel="icon"
type="image/x-icon"
/>
<style>
body {
margin: auto;
max-width: 60em;
text-align: center;
}
nav {
display: flex;
justify-content: space-evenly;
}
</style>
<script>
<%= ZOHO_JAVASCRIPT %>
</script>
<script type="module">
import hotwiredTurbo from 'https://cdn.skypack.dev/@hotwired/turbo';
</script>
</head>
<body>
<%= yield %>
<div id='zsiqwidget' data-turbo-permanent>
<!--
Rendering this element manually to avoid `d.write("<div id='zsiqwidget'></div>`
as per https://www.zoho.com/salesiq/help/getting-started-share-code-with-webmaster-websites-page-v1.html
Live Chat Code snippet as using this JavaScript API violates best practices.
-->
<!-- Zoho chat widget should inject DOM elements into this container element. -->
</div>
</body>
</html>
HTML
end
template :page do
<<~ERB
<h1>Random Dune quote</h1>
<blockquote><%= @quote %></blockquote>
<nav>
<a href="<%= @previous %>">← Previous</a>
<%= @id %>. Page
<a href="<%= @next %>">Next →</a>
</nav>
ERB
end
get '/pages/:id' do |id|
sleep rand(0.3..1.0) # slow down the site a little to actually notice that it refreshes
@id = id.to_i
@previous = url("/pages/#{@id - 1}")
@next = url("/pages/#{@id + 1}")
@quote = quotes[@id]
erb :page
end
get '/' do
redirect to('pages/1')
end
end
app.run!