-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
page.go
201 lines (154 loc) · 3.9 KB
/
page.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package app
import (
"net/url"
"strings"
)
// Page is the interface that describes a web page.
type Page interface {
// Returns the page title.
Title() string
// Sets the page title.
SetTitle(string)
// Returns the page description.
Description() string
// Sets the page description.
SetDescription(string)
// Returns the page author.
Author() string
// Sets the page author.
SetAuthor(string)
// Returns the page keywords.
Keywords() string
// Sets the page keywords.
SetKeywords(...string)
// Set the page loading label.
SetLoadingLabel(string)
// Returns the image used by social networks when linking the page.
Image() string
// Set the image used by social networks when linking the page.
SetImage(string)
// Returns the page URL.
URL() *url.URL
// Replace the the current page URL by the given one in the browser history.
//
// Does not work when pre-rendering.
ReplaceURL(*url.URL)
// Returns the page width and height in px.
Size() (w int, h int)
}
type requestPage struct {
title string
description string
author string
keywords string
loadingLabel string
image string
url *url.URL
width int
height int
}
func (p *requestPage) Title() string {
return p.title
}
func (p *requestPage) SetTitle(v string) {
p.title = v
}
func (p *requestPage) Description() string {
return p.description
}
func (p *requestPage) SetDescription(v string) {
p.description = v
}
func (p *requestPage) Author() string {
return p.author
}
func (p *requestPage) SetAuthor(v string) {
p.author = v
}
func (p *requestPage) Keywords() string {
return p.keywords
}
func (p *requestPage) SetKeywords(v ...string) {
p.keywords = strings.Join(v, ", ")
}
func (p *requestPage) SetLoadingLabel(v string) {
p.loadingLabel = v
}
func (p *requestPage) Image() string {
return p.image
}
func (p *requestPage) SetImage(v string) {
p.image = v
}
func (p *requestPage) URL() *url.URL {
return p.url
}
func (p *requestPage) ReplaceURL(v *url.URL) {
p.url = v
}
func (p *requestPage) Size() (width int, height int) {
return p.width, p.height
}
type browserPage struct {
url *url.URL
dispatcher Dispatcher
}
func (p browserPage) Title() string {
return Window().
Get("document").
Get("title").String()
}
func (p browserPage) SetTitle(v string) {
Window().Get("document").Set("title", v)
p.metaByProperty("og:title").setAttr("content", v)
}
func (p browserPage) Description() string {
return p.metaByName("description").getAttr("content")
}
func (p browserPage) SetDescription(v string) {
p.metaByName("description").setAttr("content", v)
p.metaByProperty("og:description").setAttr("content", v)
}
func (p browserPage) Author() string {
return p.metaByName("author").getAttr("content")
}
func (p browserPage) SetAuthor(v string) {
p.metaByName("author").setAttr("content", v)
}
func (p browserPage) Keywords() string {
return p.metaByName("keywords").getAttr("content")
}
func (p browserPage) SetKeywords(v ...string) {
p.metaByName("keywords").setAttr("content", strings.Join(v, ", "))
}
func (p browserPage) SetLoadingLabel(v string) {
}
func (p browserPage) Image() string {
return p.metaByProperty("og:image").getAttr("content")
}
func (p browserPage) SetImage(v string) {
p.metaByProperty("og:image").setAttr("content", p.dispatcher.resolveStaticResource(v))
}
func (p browserPage) URL() *url.URL {
if p.url != nil {
return p.url
}
return Window().URL()
}
func (p browserPage) ReplaceURL(v *url.URL) {
Window().replaceHistory(v)
p.metaByProperty("og:url").setAttr("content", v.String())
}
func (p browserPage) Size() (width int, height int) {
return Window().Size()
}
func (p browserPage) metaByName(v string) Value {
return Window().
Get("document").
Call("querySelector", "meta[name='"+v+"']")
}
func (p browserPage) metaByProperty(v string) Value {
return Window().
Get("document").
Call("querySelector", "meta[property='"+v+"']")
}