Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- name: sailfish-webview
- description: Sailfish OS WebView component implementation guidance. Use when developing Sailfish OS applications that need to display web content or integrate web-based features.
- ---
- # Sailfish Webview Skill
- ## Overview
- 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.
- ## Setup and Build Requirements
- ### Required Dependencies
- **Spec File:**
- Add this line to your `.spec` file:
- ```
- BuildRequires: pkgconfig(Qt5MozEmbed)
- ```
- **Pro File:**
- Add these lines to your `.pro` file:
- ```qmake
- QT += webview
- CONFIG += sailfishapp
- ```
- **PkgConfig:**
- Add one of these options to your `.pro` file:
- ```qmake
- PKGCONFIG += sailfish-webview
- # OR
- PKGCONFIG += qtmozembed-qt5
- ```
- ## QML API
- The QML API is provided through the `Sailfish.WebView` import namespace.
- ### Available QML Types
- - **WebView** - Main web view component
- - **WebViewFlickable** - WebView wrapped in a SilicaFlickable
- - **WebViewPage** - WebView wrapped in a Page
- - **WebEngine** - Web engine interface
- - **WebEngineSettings** - Web engine settings
- ## Basic Usage
- ### Simple WebView
- ```qml
- import Sailfish.WebView 1.0
- import Sailfish.Silica 1.0
- Page {
- WebView {
- url: "https://example.com"
- width: parent.width
- height: parent.height
- }
- }
- ```
- ### WebViewFlickable (Recommended)
- ```qml
- import Sailfish.WebView 1.0
- import Sailfish.Silica 1.0
- SilicaFlickable {
- WebViewFlickable {
- width: parent.width
- height: parent.height
- url: "https://example.com"
- }
- }
- ```
- ### WebViewPage
- ```qml
- import Sailfish.WebView 1.0
- import Sailfish.Silica 1.0
- Page {
- WebViewPage {
- url: "https://example.com"
- }
- }
- ```
- ## Properties
- ### Common WebView Properties
- - `url` - The URL to load
- - `contentHeight` - Content height of the page
- - `contentWidth` - Content width of the page
- - `loading` - Boolean indicating if page is loading
- - `progress` - Loading progress (0-100)
- - `title` - Page title
- - `icon` - Page icon
- ### Example with Properties
- ```qml
- WebView {
- url: "https://example.com"
- onLoadingChanged: {
- if (loading) {
- console.log("Loading: " + url)
- }
- }
- onProgressChanged: {
- console.log("Progress: " + progress + "%")
- }
- onTitleChanged: {
- console.log("Title: " + title)
- }
- }
- ```
- ## Loading and Navigation
- ### Navigation Methods
- - `reload()` - Reload the current page
- - `stop()` - Stop loading the current page
- - `goBack()` - Navigate to previous page
- - `goForward()` - Navigate to next page
- ### Navigation Example
- ```qml
- Button {
- text: "Back"
- enabled: webView.canGoBack
- onClicked: webView.goBack()
- }
- Button {
- text: "Forward"
- enabled: webView.canGoForward
- onClicked: webView.goForward()
- }
- Button {
- text: "Reload"
- onClicked: webView.reload()
- }
- ```
- ## Popup Customization
- WebView supports customization of user interaction dialogs via the `popupProvider` property.
- ### Required Interface
- Implement one of these interfaces:
- - **UserPromptInterface** - Custom user prompts
- - **PromptPopupInterface** - Custom prompt popups
- - **ContextMenuInterface** - Custom context menus
- - **AlertPopupInterface** - Custom alert dialogs
- - **ConfirmPopupInterface** - Custom confirmation dialogs
- - **AuthPopupInterface** - Custom authentication dialogs
- - **PasswordManagerPopupInterface** - Custom password manager dialogs
- - **LocationPopupInterface** - Custom location permission dialogs
- - **WebrtcPermissionInterface** - Custom WebRTC permission dialogs
- - **BlockedTabPopupInterface** - Custom blocked tab dialogs
- - **SelectorPopupInterface** - Custom selector popups
- ### PopupProvider Example
- ```qml
- import Sailfish.WebView 1.0
- // Your popup implementations
- QtObject {
- // Implement required signals/methods
- }
- WebView {
- url: "https://example.com"
- popupProvider: myPopupProvider
- }
- ```
- ## WebEngineSettings
- Access and modify web engine settings:
- ```qml
- import Sailfish.WebView 1.0
- WebView {
- url: "https://example.com"
- WebEngineSettings {
- javaScriptEnabled: true
- localStorageEnabled: true
- offlineStorageDatabaseEnabled: true
- autoLoadImages: true
- pluginsEnabled: true
- }
- }
- ```
- ### Common Settings
- - `javaScriptEnabled` - Enable JavaScript execution
- - `localStorageEnabled` - Enable local storage
- - `offlineStorageDatabaseEnabled` - Enable offline storage
- - `autoLoadImages` - Automatically load images
- - `pluginsEnabled` - Enable browser plugins
- ## CORS and Local Resources
- ### CORS Issues
- 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.
- **Important:** File URIs are served without headers, which can cause CORS violations.
- ### Solutions for Local Files
- #### Option 1: Serve from Remote Server (Recommended)
- Serve your app from a remote server. The app will:
- - Load from remote server on first run
- - Run locally with caching and Web Storage
- - Allow remote updates without app store submission
- #### Option 2: Local HTTP Server
- Run a local web server to control headers and CORS headers:
- **Qt-based options:**
- - QtHttpServer
- - QtWebApp
- **Python example:**
- ```python
- import http.server
- import socketserver
- PORT = 8000
- with socketserver.TCPServer(('', PORT), http.server.SimpleHTTPRequestHandler) as httpd:
- print(f"Serving at port {PORT}")
- httpd.serve_forever()
- ```
- #### Option 3: Disable CORS Checks (Not Recommended)
- Only use if you know all requests are local and trusted:
- ```qml
- WebEngineSettings {
- securityDisableCorsChecks: true
- securityDisableStrictOriginPolicy: true
- }
- ```
- **Warning:** This disables a security feature and should only be used when absolutely necessary.
- ## Debugging
- ### Console Output
- Run your application with environment variables:
- ```bash
- # Enable console output
- EMBED_CONSOLE=1
- # Show network requests
- EMBED_CONSOLE=network
- ```
- Console messages will be printed to the terminal when EMBED_CONSOLE is enabled.
- ## Best Practices
- 1. **Use Sailfish WebView** - Provides integrated Gecko-based browser engine
- 2. **Prefer Silica wrappers** - Use WebViewFlickable or WebViewPage for better integration
- 3. **Handle loading states** - Monitor `loading` and `progress` properties
- 4. **CORS aware** - Design apps to work with CORS restrictions
- 5. **Remote server preferred** - For local file serving, use a local HTTP server
- 6. **Disable CORS only when necessary** - Be aware of security implications
- 7. **Test on device** - Browser behavior may differ between platforms
- 8. **Update URL state** - Consider updating app URL when page URL changes
- 9. **Handle navigation** - Implement back/forward buttons for user convenience
- 10. **Enable JavaScript** - Enable JS for modern web apps, but be aware of security implications
- ## Common Patterns
- ### Navigation Bar
- ```qml
- Page {
- WebViewPage {
- id: webView
- url: "https://example.com"
- }
- DockedPanel {
- DockedPanel {
- id: navPanel
- width: parent.width
- height: 80
- contentHeight: 80
- dock: Dock.Bottom
- Row {
- anchors.centerIn: parent
- Button {
- text: "← Back"
- enabled: webView.canGoBack
- onClicked: webView.goBack()
- }
- Button {
- text: "→ Forward"
- enabled: webView.canGoForward
- onClicked: webView.goForward()
- }
- Button {
- text: "↻ Reload"
- onClicked: webView.reload()
- }
- }
- }
- }
- }
- ```
- ### Progress Indicator
- ```qml
- Page {
- WebViewPage {
- id: webView
- url: "https://example.com"
- }
- BusyIndicator {
- id: loadingIndicator
- anchors.centerIn: parent
- size: BusyIndicator.Size.Large
- running: webView.loading
- visible: webView.loading
- }
- ProgressBar {
- value: webView.progress
- width: parent.width
- visible: webView.loading && webView.progress < 100
- }
- }
- ```
- ### URL Bar
- ```qml
- Page {
- Column {
- width: parent.width
- TextField {
- id: urlField
- width: parent.width
- placeholderText: "Enter URL"
- text: webView.url
- onAccepted: {
- webView.url = text
- }
- }
- Button {
- text: "Go"
- onClicked: {
- webView.url = urlField.text
- }
- }
- }
- WebViewPage {
- id: webView
- url: "https://example.com"
- }
- }
- ```
- ## Project Structure
- Standard WebView project structure:
- ```
- my-web-app/
- ├── qml/
- │ ├── main.qml # Application entry
- │ ├── pages/
Advertisement
Add Comment
Please, Sign In to add comment