Webview and external css

Post Reply
User avatar
vilian
Posts: 1006
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Contact:

Webview and external css

Post by vilian »

Webview and external css files. Is it possible?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
cnavarro
Posts: 6602
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: Webview and external css

Post by cnavarro »

Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
vilian
Posts: 1006
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Contact:

Re: Webview and external css

Post by vilian »

I tried it, but it didn't work :(
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42716
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 93 times
Been thanked: 103 times
Contact:

Re: Webview and external css

Post by Antonio Linares »

Dear Vilian,

You have to read it with memoRead() and place it inside your HTML code as explained above

If it is a remote CSS file then you have to download it using FWH WebPageContents( cUrl, lText, nSysWait ) and then StrTran() it into the HTML
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
cmsoft
Posts: 1307
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina
Has thanked: 2 times
Been thanked: 4 times

Re: Webview and external css

Post by cmsoft »

Vilian, es un css personalizado? O uno estandar como bootstrap o tailwind?
Aqui uso uno web y funciona muy bien
https://www.fivetechsupport.com/forums/ ... 75#p273775
Sino, como dice antonio, leerlo con Memoread e inscrustarlo en el codigo
User avatar
cnavarro
Posts: 6602
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: Webview and external css

Post by cnavarro »

Dear Vilian,
YES, Is it possible to load a local style file in the webview
It is not necessary to load the HTML code or the contents of the CSS file into a variable, nor to use a web server (although it is recommended for many other uses in combination with the webview).
I've attached the code for the prg you should use and the HTML, CSS to use.

Please replace the path shown in the examples with the correct path where your files are located.

Try it and let me know if it worked for you.

Code: Select all | Expand


#include "fivewin.ch"

static oWebView

function Main()

   local oWnd
   
   DEFINE WINDOW oWnd TITLE "Test External CSS"

   ACTIVATE WINDOW oWnd MAXIMIZED ;
      ON INIT CreaWebView( oWnd ) ;
      ON RESIZE if( hb_IsObject( oWebView ), oWebView:SetSize( nWidth, nHeight ), )

return nil

Function CreaWebView( oWnd )

   oWebView = TWebView2():New( oWnd )

   oWebView:OpenDevToolsWindow(.T.)
   oWebView:Navigate( 'file:///d:/fwh/fwhteam/samples/vilian/index1.html' )

Return oWebView

HTML file ( index1.html )

Code: Select all | Expand

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebView con CSS</title>

<script>
function loadCSS(url) {
    const link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = url;
    link.type = "text/css";
    console.log( "Cargando CSS" );

    link.onload = function() {
        console.log(`Archivo CSS cargado: ${url}`);
    };

    link.onerror = function() {
        console.error(`Error al cargar el archivo CSS: ${url}`);
    };

   // Simula un retraso para depuración
    setTimeout(() => {
        console.log('Verificando carga...');
    }, 2000);
    document.head.appendChild(link); // Agrega el CSS al <head>
}

// Uso
   loadCSS('file:///d:/fwh/fwhteam/samples/vilian/styles.css');

</script>
</head>
<body>
    <h1>Hola desde WebView</h1>
    <p>Este texto está estilizado con CSS.</p>
</body>
</html>
CSS file ( styles.css )

Code: Select all | Expand

body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    color: #333;
}

h1 {
    color: #ff0000;
}

p {
    color: #0000ff;
}
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
Posts: 6602
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: Webview and external css

Post by cnavarro »

As soon as I have some time, I will publish an example of how to integrate a web server and webview into a graphical interface, all done with fivewin and harbor, obviously.
Look the message bar of images
Image
Image
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
Silvio.Falconi
Posts: 7232
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 16 times

Re: Webview and external css

Post by Silvio.Falconi »

I use
LOCAL cCss := BuildCss()

on this function I insert the css sample

Code: Select all | Expand


 TEXT INTO cCss
      html, body {
         font-family: Tahoma, sans-serif;
         background-color: #F5F5EB;
         padding: 20px 5px 10px 5px;
         margin: 0;
         height: 100vh;
         overflow: hidden;
      }
      .container {
         display: flex;

then on html insert

<style>
{CSS}
</style>

at the end of the file html write

</body>
</html>
ENDTEXT
cHtml := StrTran(cHtml, "{CSS}", cCss)


and run ok
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
cnavarro
Posts: 6602
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: Webview and external css

Post by cnavarro »

Obviously
The issue arises when you want to use your own stylesheet because your development is a bit more ambitious, or you "copy" a web page ( or entire site web ) from a project to run it in your webview.
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
vilian
Posts: 1006
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Contact:

Re: Webview and external css

Post by vilian »

Thank you Guys. I'll try it.
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Post Reply