Guest User

Untitled

a guest
Jun 8th, 2026
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 9.31 KB | None | 0 0
  1. ---
  2. name: sailfish-webview
  3. description: Sailfish OS WebView component implementation guidance. Use when developing Sailfish OS applications that need to display web content or integrate web-based features.
  4. ---
  5.  
  6. # Sailfish Webview Skill
  7.  
  8. ## Overview
  9.  
  10. The Sailfish WebView provides a straightforward and flexible way to introduce access to web pages and websites into your Sailfish OS application. It utilizes the Sailfish OS browser engine built using Mozilla Gecko.
  11.  
  12. ## Setup and Build Requirements
  13.  
  14. ### Required Dependencies
  15.  
  16. **Spec File:**
  17. Add this line to your `.spec` file:
  18. ```
  19. BuildRequires:   pkgconfig(Qt5MozEmbed)
  20. ```
  21.  
  22. **Pro File:**
  23. Add these lines to your `.pro` file:
  24. ```qmake
  25. QT += webview
  26. CONFIG += sailfishapp
  27. ```
  28.  
  29. **PkgConfig:**
  30. Add one of these options to your `.pro` file:
  31. ```qmake
  32. PKGCONFIG += sailfish-webview
  33. # OR
  34. PKGCONFIG += qtmozembed-qt5
  35. ```
  36.  
  37. ## QML API
  38.  
  39. The QML API is provided through the `Sailfish.WebView` import namespace.
  40.  
  41. ### Available QML Types
  42.  
  43. - **WebView** - Main web view component
  44. - **WebViewFlickable** - WebView wrapped in a SilicaFlickable
  45. - **WebViewPage** - WebView wrapped in a Page
  46. - **WebEngine** - Web engine interface
  47. - **WebEngineSettings** - Web engine settings
  48.  
  49. ## Basic Usage
  50.  
  51. ### Simple WebView
  52.  
  53. ```qml
  54. import Sailfish.WebView 1.0
  55. import Sailfish.Silica 1.0
  56.  
  57. Page {
  58.     WebView {
  59.         url: "https://example.com"
  60.         width: parent.width
  61.         height: parent.height
  62.     }
  63. }
  64. ```
  65.  
  66. ### WebViewFlickable (Recommended)
  67.  
  68. ```qml
  69. import Sailfish.WebView 1.0
  70. import Sailfish.Silica 1.0
  71.  
  72. SilicaFlickable {
  73.     WebViewFlickable {
  74.         width: parent.width
  75.         height: parent.height
  76.         url: "https://example.com"
  77.     }
  78. }
  79. ```
  80.  
  81. ### WebViewPage
  82.  
  83. ```qml
  84. import Sailfish.WebView 1.0
  85. import Sailfish.Silica 1.0
  86.  
  87. Page {
  88.     WebViewPage {
  89.         url: "https://example.com"
  90.     }
  91. }
  92. ```
  93.  
  94. ## Properties
  95.  
  96. ### Common WebView Properties
  97.  
  98. - `url` - The URL to load
  99. - `contentHeight` - Content height of the page
  100. - `contentWidth` - Content width of the page
  101. - `loading` - Boolean indicating if page is loading
  102. - `progress` - Loading progress (0-100)
  103. - `title` - Page title
  104. - `icon` - Page icon
  105.  
  106. ### Example with Properties
  107.  
  108. ```qml
  109. WebView {
  110.     url: "https://example.com"
  111.  
  112.     onLoadingChanged: {
  113.         if (loading) {
  114.             console.log("Loading: " + url)
  115.         }
  116.     }
  117.  
  118.     onProgressChanged: {
  119.         console.log("Progress: " + progress + "%")
  120.     }
  121.  
  122.     onTitleChanged: {
  123.         console.log("Title: " + title)
  124.     }
  125. }
  126. ```
  127.  
  128. ## Loading and Navigation
  129.  
  130. ### Navigation Methods
  131.  
  132. - `reload()` - Reload the current page
  133. - `stop()` - Stop loading the current page
  134. - `goBack()` - Navigate to previous page
  135. - `goForward()` - Navigate to next page
  136.  
  137. ### Navigation Example
  138.  
  139. ```qml
  140. Button {
  141.     text: "Back"
  142.     enabled: webView.canGoBack
  143.  
  144.     onClicked: webView.goBack()
  145. }
  146.  
  147. Button {
  148.     text: "Forward"
  149.     enabled: webView.canGoForward
  150.  
  151.     onClicked: webView.goForward()
  152. }
  153.  
  154. Button {
  155.     text: "Reload"
  156.     onClicked: webView.reload()
  157. }
  158. ```
  159.  
  160. ## Popup Customization
  161.  
  162. WebView supports customization of user interaction dialogs via the `popupProvider` property.
  163.  
  164. ### Required Interface
  165.  
  166. Implement one of these interfaces:
  167.  
  168. - **UserPromptInterface** - Custom user prompts
  169. - **PromptPopupInterface** - Custom prompt popups
  170. - **ContextMenuInterface** - Custom context menus
  171. - **AlertPopupInterface** - Custom alert dialogs
  172. - **ConfirmPopupInterface** - Custom confirmation dialogs
  173. - **AuthPopupInterface** - Custom authentication dialogs
  174. - **PasswordManagerPopupInterface** - Custom password manager dialogs
  175. - **LocationPopupInterface** - Custom location permission dialogs
  176. - **WebrtcPermissionInterface** - Custom WebRTC permission dialogs
  177. - **BlockedTabPopupInterface** - Custom blocked tab dialogs
  178. - **SelectorPopupInterface** - Custom selector popups
  179.  
  180. ### PopupProvider Example
  181.  
  182. ```qml
  183. import Sailfish.WebView 1.0
  184.  
  185. // Your popup implementations
  186. QtObject {
  187.     // Implement required signals/methods
  188. }
  189.  
  190. WebView {
  191.     url: "https://example.com"
  192.     popupProvider: myPopupProvider
  193. }
  194. ```
  195.  
  196. ## WebEngineSettings
  197.  
  198. Access and modify web engine settings:
  199.  
  200. ```qml
  201. import Sailfish.WebView 1.0
  202.  
  203. WebView {
  204.     url: "https://example.com"
  205.  
  206.     WebEngineSettings {
  207.         javaScriptEnabled: true
  208.         localStorageEnabled: true
  209.         offlineStorageDatabaseEnabled: true
  210.         autoLoadImages: true
  211.         pluginsEnabled: true
  212.     }
  213. }
  214. ```
  215.  
  216. ### Common Settings
  217.  
  218. - `javaScriptEnabled` - Enable JavaScript execution
  219. - `localStorageEnabled` - Enable local storage
  220. - `offlineStorageDatabaseEnabled` - Enable offline storage
  221. - `autoLoadImages` - Automatically load images
  222. - `pluginsEnabled` - Enable browser plugins
  223.  
  224. ## CORS and Local Resources
  225.  
  226. ### CORS Issues
  227.  
  228. When Cross-Origin Resource Sharing (CORS) is applied, resources loaded from another origin must be aware of the cross-origin boundary. The content security manager will intercept and verify requests, and deny them if there's a violation.
  229.  
  230. **Important:** File URIs are served without headers, which can cause CORS violations.
  231.  
  232. ### Solutions for Local Files
  233.  
  234. #### Option 1: Serve from Remote Server (Recommended)
  235.  
  236. Serve your app from a remote server. The app will:
  237. - Load from remote server on first run
  238. - Run locally with caching and Web Storage
  239. - Allow remote updates without app store submission
  240.  
  241. #### Option 2: Local HTTP Server
  242.  
  243. Run a local web server to control headers and CORS headers:
  244.  
  245. **Qt-based options:**
  246. - QtHttpServer
  247. - QtWebApp
  248.  
  249. **Python example:**
  250. ```python
  251. import http.server
  252. import socketserver
  253.  
  254. PORT = 8000
  255.  
  256. with socketserver.TCPServer(('', PORT), http.server.SimpleHTTPRequestHandler) as httpd:
  257.    print(f"Serving at port {PORT}")
  258.    httpd.serve_forever()
  259. ```
  260.  
  261. #### Option 3: Disable CORS Checks (Not Recommended)
  262.  
  263. Only use if you know all requests are local and trusted:
  264.  
  265. ```qml
  266. WebEngineSettings {
  267.    securityDisableCorsChecks: true
  268.    securityDisableStrictOriginPolicy: true
  269. }
  270. ```
  271.  
  272. **Warning:** This disables a security feature and should only be used when absolutely necessary.
  273.  
  274. ## Debugging
  275.  
  276. ### Console Output
  277.  
  278. Run your application with environment variables:
  279.  
  280. ```bash
  281. # Enable console output
  282. EMBED_CONSOLE=1
  283.  
  284. # Show network requests
  285. EMBED_CONSOLE=network
  286. ```
  287.  
  288. Console messages will be printed to the terminal when EMBED_CONSOLE is enabled.
  289.  
  290. ## Best Practices
  291.  
  292. 1. **Use Sailfish WebView** - Provides integrated Gecko-based browser engine
  293. 2. **Prefer Silica wrappers** - Use WebViewFlickable or WebViewPage for better integration
  294. 3. **Handle loading states** - Monitor `loading` and `progress` properties
  295. 4. **CORS aware** - Design apps to work with CORS restrictions
  296. 5. **Remote server preferred** - For local file serving, use a local HTTP server
  297. 6. **Disable CORS only when necessary** - Be aware of security implications
  298. 7. **Test on device** - Browser behavior may differ between platforms
  299. 8. **Update URL state** - Consider updating app URL when page URL changes
  300. 9. **Handle navigation** - Implement back/forward buttons for user convenience
  301. 10. **Enable JavaScript** - Enable JS for modern web apps, but be aware of security implications
  302.  
  303. ## Common Patterns
  304.  
  305. ### Navigation Bar
  306.  
  307. ```qml
  308. Page {
  309.    WebViewPage {
  310.        id: webView
  311.        url: "https://example.com"
  312.    }
  313.  
  314.    DockedPanel {
  315.        DockedPanel {
  316.            id: navPanel
  317.            width: parent.width
  318.            height: 80
  319.            contentHeight: 80
  320.            dock: Dock.Bottom
  321.  
  322.            Row {
  323.                anchors.centerIn: parent
  324.  
  325.                Button {
  326.                    text: "← Back"
  327.                    enabled: webView.canGoBack
  328.                    onClicked: webView.goBack()
  329.                }
  330.  
  331.                Button {
  332.                    text: "→ Forward"
  333.                    enabled: webView.canGoForward
  334.                    onClicked: webView.goForward()
  335.                }
  336.  
  337.                Button {
  338.                    text: "↻ Reload"
  339.                    onClicked: webView.reload()
  340.                }
  341.            }
  342.        }
  343.    }
  344. }
  345. ```
  346.  
  347. ### Progress Indicator
  348.  
  349. ```qml
  350. Page {
  351.    WebViewPage {
  352.        id: webView
  353.        url: "https://example.com"
  354.    }
  355.  
  356.    BusyIndicator {
  357.        id: loadingIndicator
  358.        anchors.centerIn: parent
  359.        size: BusyIndicator.Size.Large
  360.        running: webView.loading
  361.        visible: webView.loading
  362.    }
  363.  
  364.    ProgressBar {
  365.        value: webView.progress
  366.        width: parent.width
  367.        visible: webView.loading && webView.progress < 100
  368.    }
  369. }
  370. ```
  371.  
  372. ### URL Bar
  373.  
  374. ```qml
  375. Page {
  376.    Column {
  377.        width: parent.width
  378.  
  379.        TextField {
  380.            id: urlField
  381.            width: parent.width
  382.            placeholderText: "Enter URL"
  383.            text: webView.url
  384.  
  385.            onAccepted: {
  386.                webView.url = text
  387.            }
  388.        }
  389.  
  390.        Button {
  391.            text: "Go"
  392.            onClicked: {
  393.                webView.url = urlField.text
  394.            }
  395.        }
  396.    }
  397.  
  398.    WebViewPage {
  399.        id: webView
  400.        url: "https://example.com"
  401.    }
  402. }
  403. ```
  404.  
  405. ## Project Structure
  406.  
  407. Standard WebView project structure:
  408. ```
  409. my-web-app/
  410. ├── qml/
  411. │   ├── main.qml              # Application entry
  412. │   ├── pages/
Advertisement
Add Comment
Please, Sign In to add comment