forked from baldo/haskell-webkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.hs
163 lines (113 loc) · 4.82 KB
/
Test.hs
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
module Main where
import System
import System.Glib.Signals
import System.Glib.GType
import Graphics.UI.Gtk
import Graphics.UI.Gtk.Glade
import Graphics.UI.Gtk.WebKit
import Graphics.UI.Gtk.WebKit.General.Types
import Language.JavaScript.JavaScriptCore
import Network.Soup
import Graphics.UI.Gtk.Abstract.Object
( makeNewObject
)
main = startBrowser
startBrowser :: IO ()
startBrowser = do
-- Init -------------------------------------------------------------------
initWebKit
-- Load GUI ---------------------------------------------------------------
Just xml <- xmlNew "test.glade"
wMain <- xmlGetWidget xml castToWindow "wMain"
vbBrowser <- xmlGetWidget xml castToVBox "vbBrowser"
swBrowser <- xmlGetWidget xml castToScrolledWindow "swBrowser"
eAddress <- xmlGetWidget xml castToEntry "eAddress"
sbMain <- xmlGetWidget xml castToStatusbar "sbMain"
tbQuit <- xmlGetWidget xml castToToolButton "tbQuit"
tbPrint <- xmlGetWidget xml castToToolButton "tbPrint"
tbGo <- xmlGetWidget xml castToToolButton "tbGo"
tbStop <- xmlGetWidget xml castToToolButton "tbStop"
tbReload <- xmlGetWidget xml castToToolButton "tbReload"
tbBack <- xmlGetWidget xml castToToolButton "tbBack"
tbForward <- xmlGetWidget xml castToToolButton "tbForward"
imiQuit <- xmlGetWidget xml castToImageMenuItem "imiQuit"
-- Create WebView ---------------------------------------------------------
wvBrowser <- webViewNew
containerAdd swBrowser wvBrowser
-- Events -----------------------------------------------------------------
onDestroy wMain mainQuit
onToolButtonClicked tbQuit $ widgetDestroy wMain
onToolButtonClicked tbPrint $
webViewGetMainFrame wvBrowser >>= webFramePrint
onToolButtonClicked tbGo $ do
widgetGrabFocus wvBrowser
loadAddress eAddress wvBrowser
onToolButtonClicked tbStop $ webViewStopLoading wvBrowser
onToolButtonClicked tbReload $ webViewReload wvBrowser
onToolButtonClicked tbBack $ do
widgetGrabFocus wvBrowser
webViewGoBackOrForward wvBrowser (-1)
onToolButtonClicked tbForward $ do
widgetGrabFocus wvBrowser
webViewGoForward wvBrowser
onEntryActivate eAddress $ do
widgetGrabFocus wvBrowser
loadAddress eAddress wvBrowser
onActivateLeaf imiQuit $ widgetDestroy wMain
-- WebKit Events ----------------------------------------------------------
--onWebViewStatusbarTextChanged wvBrowser $
-- \text -> putStrLn $ "SB: " ++ text
onWebViewCopyClipboard wvBrowser $
putStrLn "Copy"
onWebViewResourceRequestStarting wvBrowser $
\ wv wf wr nreq _ -> do
putStrLn "onWebViewResourceRequestStarting"
nr <- networkRequestGetUri nreq
mime <- webResourceGetMimeType wr
encoding <- webResourceGetEncoding wr
putStrLn $ "-- URI: " ++ nr
putStrLn $ "-- MIME: " ++ (show mime)
putStrLn $ "-- Encoding: " ++ (show encoding)
onWebViewStatusbarTextChanged wvBrowser $
(\ x -> putStrLn $ "onWebViewStatusbarTextChanged" ++ x)
onWebViewScriptPrompt wvBrowser $
\ wv wf message def text -> do
putStrLn "onWebViewScriptPrompt"
webViewGetUri wv >>= print
putStrLn message
putStrLn def
putStrLn text
return False
onWebViewDownloadRequested wvBrowser $
\ webView download -> do
putStrLn "onWebViewDownloadRequested"
return True
onWebViewNavigationPolicyDecisionRequested wvBrowser $
\ webView webFrame networkRequest webNavigationAction webPolicyDecision ->
putStrLn "onWebViewNavigationPolicyDecisionRequested"
onWebViewMimeTypePolicyDecisionRequested wvBrowser $
\ webView webFrame networkRequest mimetype policyDecision -> do
putStrLn "onWebViewMimeTypePolicyDecisionRequested"
putStrLn $ "-- MIME: " ++ mimetype
webPolicyDecisionUse policyDecision
return True
-- Show and run GUI -------------------------------------------------------
widgetSetSensitive tbStop False
loadAddress eAddress wvBrowser
widgetGrabFocus wvBrowser
widgetShowAll wMain
mainGUI
loadAddress :: Entry -> WebView -> IO ()
loadAddress eAddress wvBrowser = do
uri <- entryGetText eAddress
wfBrowser <- webViewGetMainFrame wvBrowser
webViewLoadUri wvBrowser uri
-- webViewGetTitle wvBrowser >>= print
-- webViewGetUri wvBrowser >>= print
-- webViewGetProgress wvBrowser >>= print
-- webViewGetLoadStatus wvBrowser >>= print
-- entrySetText eAddress newUri
ws <- webSettingsNew
-- webSettingsGetUserAgent ws >>= print
webViewSetSettings wvBrowser ws
return ()