Stop WebGadget accessing the internet

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Stop WebGadget accessing the internet

Post by BarryG »

RASHAD wrote:Hi BarryG
The next snippet did not fail never once
Check and report
No good, sorry Rashad. Here's a screenshot with a local HTML file that I'm testing with -> https://i.imgur.com/DiJ4IsX.png . See how it shows the "wait" cursor at bottom-left after clicking the button? It does that for about 20-30 seconds before showing the page, because it's still able to download all the unwanted third-party external data.

For comparison, here's the same page when I've unplugged my ethernet cable, which shows the page instantly when I click the button because downloading of external data is not possible -> https://i.imgur.com/l6Otren.png
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Stop WebGadget accessing the internet

Post by RASHAD »

Sorry BarryG
I am using WIFI connection :)
Egypt my love
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Stop WebGadget accessing the internet

Post by BarryG »

That's okay. It's a tough issue, and I literally can't find any tips online, on any coding sites, on how to temporarily prevent an already-running exe from accessing the internet. You'd think there'd be a way!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Stop WebGadget accessing the internet

Post by RASHAD »

Hi BarryG
Try :
https://www.sordum.org/downloads/?st-net-disablerocker
It has command line too
Egypt my love
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Stop WebGadget accessing the internet

Post by BarryG »

I know about that one. It requires admin rights to work, and cuts the entire internet connection, not just for my app. So, no good. Thanks anyway!

Don't worry about it anymore; I've given up on finding a solution. It obviously can't be done or I would've found an online tutorial by now.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Stop WebGadget accessing the internet

Post by RASHAD »

- Run your application as admin
Then

Code: Select all

   Case #Button_Start
          ShellExecute_(#Null, @"open", @"c:\users\mmras\NetDisabler_x64.exe", @" /D", @"C:\users\mmras\", #SW_NORMAL)
          web$ = GetHomeDirectory()+"webpage.html"
          SetGadgetText(#Web_0, web$)  ;;  full path to local html file
- Run

Code: Select all

ShellExecute_(#Null, @"open", @"c:\users\mmras\NetDisabler_x64.exe", @" /E", @"C:\users\mmras\", #SW_NORMAL)
Only one request for admin will be required at start
Egypt my love
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Stop WebGadget accessing the internet

Post by firace »

I investigated a little bit more.
The hanging is caused by the CSS file, which unfortunately does not seem to obey DLCTL directives.
In your example page the file is named bundle.css.
A quick hack to disable the loading of the CSS file is to rename it to a different filename (eg, disabledbundle.css)
Then the page always loads instantly in my tests.

(If you'd prefer to preserve most styles in that file, there's another quick hack which I'm still testing at the moment)
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Stop WebGadget accessing the internet

Post by BarryG »

I can't disable the files, because I don't want to modify the user's data like that (even temporarily), and they also might reside on a read-only media (CD-ROM) anyway. And the user won't always have admin rights.
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Stop WebGadget accessing the internet

Post by ricardo »

Code: Select all


Structure INTERNET_PROXY_INFO1
  dwAccessType.l
  lpszProxy.l
  lpszProxyBypass.l
EndStructure

#INTERNET_OPTION_PROXY = 38

ProcedureDLL SetProxy1(Proxy.S, Port.l, flags.l=#INTERNET_OPEN_TYPE_PROXY) ; Set proxy for the current session
  ;/ Return 1 if success or 0 if fail
  
  Protected ProxyServer.S = Proxy + ":" + Str(Port)
  Protected PIInfo.INTERNET_PROXY_INFO1
  PIInfo\dwAccessType = flags
  PIInfo\lpszProxy = @ProxyServer
  PIInfo\lpszProxyBypass = @""
  If UrlMkSetSessionOption_(#INTERNET_OPTION_PROXY, @PIInfo, SizeOf(INTERNET_PROXY_INFO1), 0) = #S_OK
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

ARGENTINA WORLD CHAMPION
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Stop WebGadget accessing the internet

Post by BarryG »

ricardo, how do I call that? I tried this which (obviously) doesn't work. I know nothing about proxies.

Code: Select all

SetProxy1("",0,0)
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Stop WebGadget accessing the internet

Post by ricardo »

This one does NOT need webgadget:

Code: Select all

Procedure.S HttpRequest(url.S, ReturnType = 1, PostData.S = "", Cookie.S = "", User_agent.S = "", Referer.S = "", File_to_download.S = "", Proxy.S = "", Timeout.L = 30000, Redirect.b = #True)
  
  ;{ Format URL
  
  If FindString(url, "https://", 1, #PB_String_NoCase) 
    Is_secure.b = #True 
  EndIf
  url = RemoveString(url, "https://", #PB_String_NoCase) 
  url = RemoveString(url, "http://", #PB_String_NoCase) 
  Host.S = StringField(url, 1, "/") 
  page.S = RemoveString(url, Host, #PB_String_NoCase)   
  page.S = RTrim(page, "/")
  
  ;}
  
  ;{ Add headers
  If Not Proxy = "" : Access_type.i = 3 : Else : Access_type.i = 1 : EndIf
  If User_agent = "" : User_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" : EndIf
  Open_handle = InternetOpen_(User_agent, Access_type, Proxy, "", 0)
  InternetSetOption_(Open_handle, 2, @Timeout, 4)
  InternetSetOption_(Open_handle, 5, @Timeout, 4) ; #INTERNET_OPTION_SEND_TIMEOUT
  InternetSetOption_(Open_handle, 6, @Timeout, 4) ; #INTERNET_OPTION_RECEIVE_TIMEOUT
  InternetSetOption_(Open_handle, 7, @Timeout, 4) ; #INTERNET_OPTION_DATA_SEND_TIMEOUT
  InternetSetOption_(Open_handle, 8, @Timeout, 4) ; #INTERNET_OPTION_DATA_RECEIVE_TIMEOUT
  If Is_secure : port.i = 443 : Flag.L = $00800000|$00001000|$00002000|$00080000|$00000100|$04000000
    Else : port.i = 80 : Flag.L = $00080000|$00000100|$04000000 : EndIf
  If Not Redirect : Flag|$00200000 : EndIf
  If Not PostData = "" : Verb.S = "POST" : Else : Verb.S = "GET" : EndIf
  If page = "" : page = "/"  : EndIf
  Connect_handle = InternetConnect_(Open_handle, Host, port, "", "", 3, 0, 0)
  Request_handle = HttpOpenRequest_(Connect_handle, Verb, page, "", Referer, 0, Flag, 0)
  If Verb = "POST"
    Headers.S = "Content-Type: application/x-www-form-urlencoded" + Chr(13) + Chr(10)
    HttpAddRequestHeaders_(Request_handle, Headers, Len(Headers), $80000000|$20000000)
  EndIf
  If Not Cookie = ""
    Headers.S = "Cookie: " + Cookie + Chr(13) + Chr(10)
    HttpAddRequestHeaders_(Request_handle, Headers, Len(Headers), $80000000|$20000000)
  EndIf
  
  ;}
  
  ;{ Send request
  If #PB_Compiler_Unicode  
    *PostDataAnsi = AllocateMemory(StringByteLength(PostData, #PB_Ascii) + 1)
    PokeS(*PostDataAnsi, PostData, -1, #PB_Ascii)
    Send_handle = HttpSendRequest_(Request_handle, "", 0, *PostDataAnsi, StringByteLength(PostData, #PB_Ascii)) 
    FreeMemory(*PostDataAnsi)
  Else
    Send_handle = HttpSendRequest_(Request_handle, "", 0, PostData, Len(PostData))
  EndIf
  
  ;}
  
  ;{ Receive response
  If ReturnType = 1 ; Return server response content
    buffer.S = Space(1024)  
    Repeat
      InternetReadFile_(Request_handle, @buffer, 1024, @Bytes_read.L)
      Result1.S + Left(PeekS(@buffer,-1,#PB_Ascii), Bytes_read)
      buffer = Space(1024)
    Until Bytes_read = 0  
    
  ElseIf  ReturnType = 2; Return Cookie(s)
    For i = 0 To 9
      buffer.S = Space(1024)
      Headernum = i
      length = Len(buffer)
      If HttpQueryInfo_(Request_handle, 43, @buffer, @length, @Headernum)
        Result1.S +  buffer  + #CRLF$   
      EndIf   
    Next
    
  ElseIf  ReturnType = 3; Return both
    For i = 0 To 9
      buffer.S = Space(1024)
      Headernum = i
      length = Len(buffer)
      If HttpQueryInfo_(Request_handle, 43, @buffer, @length, @Headernum)
        Result1.S +  buffer  + #CRLF$   
      EndIf   
    Next
    Result1 + #CRLF$
    buffer.S = Space(1024)  
    Repeat
      InternetReadFile_(Request_handle, @buffer, 1024, @Bytes_read.L)
      Result1.S + Left(PeekS(@buffer,-1,#PB_Ascii), Bytes_read)
      buffer = Space(1024)
    Until Bytes_read = 0  
    
  ElseIf  ReturnType = 4; Download file
    If File_to_download <> "" 
      Filehandle = CreateFile(#PB_Any, File_to_download) 
      fBytes.L = 0
      Loop.b = 1
      fBuffer = AllocateMemory(4096)
      Repeat
        InternetReadFile_(Request_handle, fBuffer, 4096, @Bytes_read.L)
        If Bytes_read = 0
          Loop = 0
        Else
          fBytes = fBytes + Bytes_read
          WriteData(Filehandle, fBuffer, Bytes_read)          
        EndIf
      Until Loop = 0
      CloseFile(Filehandle)
      Result1 = Str(FileSize(File_to_download))
      FreeMemory(fBuffer)
    Else
      Result1 = ""
    EndIf
    
  ElseIf ReturnType = 5 ; return redirected URl + cookies
    For i = 0 To 9
      buffer.S = Space(1024)
      Headernum = i
      length = Len(buffer)
      If HttpQueryInfo_(Request_handle, 43, @buffer, @length, @Headernum)
        Result1.S +  buffer  + #CRLF$   
      EndIf   
    Next
    
    
    buffer.S = Space(#MAX_PATH)
    Headernum = 0
    length = Len(buffer)  
    If HttpQueryInfo_(Request_handle, 33, @buffer, @length, @Headernum)    
      Result1 = buffer + #CRLF$ + Result1
    EndIf       
    
  EndIf
  
  ;}
  
  ;{ Close handle
  InternetCloseHandle_(Open_handle)
  InternetCloseHandle_(Connect_handle)
  InternetCloseHandle_(Request_handle)
  InternetCloseHandle_(Send_handle) 
  Delay(70)  
  
  ;}  
  
  ProcedureReturn Result1
  
EndProcedure
The EASY way yo use a proxy in thr webgadget

Code: Select all


;You need to write a regfile, Proxy$ is tahe proxy

    RegFile$ ="REGEDIT4" + Chr(13) + Chr(10 ) + Chr(13) + Chr(10 ) 
    RegFile$ + "[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]" + Chr(13) + Chr(10 )
    RegFile$ + Chr(34) + "ProxyEnable" + Chr(34) + "=dword:00000001" + Chr(13) + Chr(10 )
    RegFile$ + Chr(34) + "ProxyServer" + Chr(34) + "=" + Chr(34) + Proxy$ + Chr(34)
    
    
    ;Then you write thiw file As a reg file
    If CreateFile(0,"includes/Reg.reg")
      WriteString(0,RegFile$)
      CloseFile(0)
    EndIf
    
    ;run it by this way to avoid warnings
    iq = RunProgram("re" + "ge" + "dit.exe"," /s " + Chr(34) + PpATH$  + "includes/Reg.reg" + Chr(34),"",#PB_Program_Open|#PB_Program_Wait)

  
;At the end you restore everything writing this in a reg file and runing it
RegFile$ ="REGEDIT4" + Chr(13) + Chr(10 ) + Chr(13) + Chr(10 ) 
RegFile$ + "[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]" + Chr(13) + Chr(10 )
RegFile$ + Chr(34) + "ProxyEnable" + Chr(34) + "=dword:00000000" + Chr(13) + Chr(10 )
ARGENTINA WORLD CHAMPION
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Stop WebGadget accessing the internet

Post by RASHAD »

Hi BarryG,

Here's another method you could try;

- Open the requested HTML file as a string.
- Replace all internal occurrences of "href", "src" attributes with empty or null value.
- Replace all internal occurrences of "<link>", "<script>" tags with empty or null value.
-Save string as new temp HTML file.
-Open file in WebGadget.

I'm already working on this, but please report back if you get there first.

EDIT: Created a really simple pass over the idea.

Code: Select all

;EnableExplicit

;------------------------------------------------------------------------------
;- * IDispatch implementation

;------------------------------------------------------------------------------
;- Constants
#DISPID_AMBIENT_DLCONTROL         = -5512

#DLCTL_DLIMAGES                   = $00000010
#DLCTL_VIDEOS                     = $00000020
#DLCTL_BGSOUNDS                   = $00000040
#DLCTL_NO_SCRIPTS                 = $00000080
#DLCTL_NO_JAVA                    = $00000100
#DLCTL_NO_RUNACTIVEXCTLS          = $00000200
#DLCTL_NO_DLACTIVEXCTLS           = $00000400
#DLCTL_DOWNLOADONLY               = $00000800
#DLCTL_NO_FRAMEDOWNLOAD           = $00001000
#DLCTL_RESYNCHRONIZE              = $00002000
#DLCTL_PRAGMA_NO_CACHE            = $00004000
#DLCTL_NO_BEHAVIORS               = $00008000
#DLCTL_NO_METACHARSET             = $00010000
#DLCTL_URL_ENCODING_DISABLE_UTF8  = $00020000
#DLCTL_URL_ENCODING_ENABLE_UTF8   = $00040000
#DLCTL_FORCEOFFLINE               = $10000000
#DLCTL_NO_CLIENTPULL              = $20000000
#DLCTL_SILENT                     = $40000000
#DLCTL_OFFLINEIFNOTCONNECTED      = $80000000
#DLCTL_OFFLINE                    = #DLCTL_OFFLINEIFNOTCONNECTED

;------------------------------------------------------------------------------
;- Structures
Structure IDispatch_Functions
  QueryInterface.i
  AddRef.i
  Release.i
  GetTypeInfoCount.i
  GetTypeInfo.i
  GetIDsOfNames.i
  Invoke.i
EndStructure

Structure IDispatch_Object
  *IDispatch.IDispatch
  RefCount.l
EndStructure

Global NewList g_IDispatch_Objects.IDispatch_Object()

;------------------------------------------------------------------------------
;- IUnknown methods

Procedure IDispatch_QueryInterface(*THIS.IDispatch_Object, *iid.IID, *Object.INTEGER)
  If *Object = 0
    ProcedureReturn #E_INVALIDARG
  ElseIf CompareMemory(*iid, ?IID_IUnknown, SizeOf(IID)) Or CompareMemory(*iid, ?IID_IDispatch, SizeOf(IID))
    *Object\i = *THIS
    *THIS\RefCount + 1
    ProcedureReturn #S_OK 
  Else
    *Object\i = 0
    ProcedureReturn #E_NOINTERFACE 
  EndIf
EndProcedure

Procedure IDispatch_AddRef(*THIS.IDispatch_Object)
  *THIS\RefCount + 1
  ProcedureReturn *THIS\RefCount
EndProcedure

Procedure IDispatch_Release(*THIS.IDispatch_Object)
  *THIS\RefCount - 1
  If *THIS\RefCount <= 0   
    ChangeCurrentElement(g_IDispatch_Objects(), *THIS)   
    DeleteElement(g_IDispatch_Objects())
    ProcedureReturn 0
  Else
    ProcedureReturn *THIS\RefCount
  EndIf
EndProcedure

;------------------------------------------------------------------------------
;- IDispatch methods

Procedure IDispatch_GetTypeInfoCount(*THIS.IDispatch_Object, *pctinfo.INTEGER)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IDispatch_GetTypeInfo(*THIS.IDispatch_Object, iTInfo.l, lcid.l, *pptInfo.INTEGER)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IDispatch_GetIDsOfNames(*THIS.IDispatch_Object, *riid.IID, rgszNames.i, cNames.l, lcid.l, *rgDispID.INTEGER)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IDispatch_Invoke(*THIS.IDispatch_Object, dispIdMember.l, *riid.IID, lcid.l, wFlags.w, *pDispParams.DISPPARAMS, *pVarResult.Variant, pExcpInfo.i, puArgErr.i)
  If dispIdMember = #DISPID_AMBIENT_DLCONTROL
    *pVarResult\vt = #VT_I4
;     *pVarResult\lVal = #DLCTL_NO_JAVA | #DLCTL_NO_DLACTIVEXCTLS | #DLCTL_NO_RUNACTIVEXCTLS | #DLCTL_SILENT
    *pVarResult\lVal = #DLCTL_OFFLINEIFNOTCONNECTED | #DLCTL_FORCEOFFLINE  | #DLCTL_NO_JAVA | #DLCTL_NO_SCRIPTS | #DLCTL_NO_DLACTIVEXCTLS | #DLCTL_NO_RUNACTIVEXCTLS | #DLCTL_SILENT
    Debug "**** IDispatch::Invoke() #DISPID_AMBIENT_DLCONTROL"
    Debug *pVarResult\lVal
    Debug "****"
    ProcedureReturn #S_OK
  EndIf
  ProcedureReturn #DISP_E_MEMBERNOTFOUND
EndProcedure

;------------------------------------------------------------------------------
;- * IOleClientSite implementation

;------------------------------------------------------------------------------
;- Structures
Structure IOleClientSite_Functions
  QueryInterface.i
  AddRef.i
  Release.i
  SaveObject.i
  GetMoniker.i
  GetContainer.i
  ShowObject.i
  OnShowWindow.i
  RequestNewObjectLayout.i
EndStructure

Structure IOleClientSite_Object
  *IOleClientSite.IOleClientSite
  RefCount.l
EndStructure

Global NewList g_IOleClientSite_Objects.IOleClientSite_Object()

;------------------------------------------------------------------------------
;- IUnknown methods

Procedure IOleClientSite_QueryInterface(*THIS.IOleClientSite_Object, *iid.IID, *Object.INTEGER)
  If *Object = 0
    ProcedureReturn #E_INVALIDARG
  ElseIf CompareMemory(*iid, ?IID_IUnknown, SizeOf(IID)) Or CompareMemory(*iid, ?IID_IOleClientSite, SizeOf(IID))
    *Object\i = *THIS
    *THIS\RefCount + 1
    ProcedureReturn #S_OK
    ; return pointer to IDispatch object (IDispatch is queried by the webbrowser control on its initialization)
  ElseIf CompareMemory(*iid, ?IID_IDispatch, SizeOf(IID))
    *Object\i = @g_IDispatch_Objects()
    ProcedureReturn #S_OK
  Else
    *Object\i = 0
    ProcedureReturn #E_NOINTERFACE
  EndIf
EndProcedure

Procedure IOleClientSite_AddRef(*THIS.IOleClientSite_Object)
  *THIS\RefCount + 1
  ProcedureReturn *THIS\RefCount
EndProcedure

Procedure IOleClientSite_Release(*THIS.IOleClientSite_Object)
  *THIS\RefCount - 1
  If *THIS\RefCount <= 0   
    ChangeCurrentElement(g_IOleClientSite_Objects(), *THIS)   
    DeleteElement(g_IOleClientSite_Objects())
    ProcedureReturn 0
  Else
    ProcedureReturn *THIS\RefCount
  EndIf
EndProcedure

;------------------------------------------------------------------------------
;- IOleClientSite methods

Procedure IOleClientSite_SaveObject(*THIS.IOleClientSite_Object)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IOleClientSite_GetMoniker(*THIS.IOleClientSite_Object, dwAssign.l, dwWhichMoniker.l, mk.i )
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IOleClientSite_GetContainer(*THIS.IOleClientSite_Object, container.i)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IOleClientSite_ShowObject(*THIS.IOleClientSite_Object)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IOleClientSite_OnShowWindow(*THIS.IOleClientSite_Object, fShow.l)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure IOleClientSite_RequestNewObjectLayout(*THIS.IOleClientSite_Object)
  ProcedureReturn #E_NOTIMPL
EndProcedure

Procedure ResizeWebgadget(gd.i, x.l, y.l, width.l, height.l, redraw.b = #True)
   Define.i hwGd
   Define.IWebBrowser2 wb
   Define.IOleObject oObj
   Define.IOleInPlaceObject ipObj
   Define.RECT rc
   
   hwGd = GadgetID(gd)
   wb = GetWindowLongPtr_(hwGd, #GWLP_USERDATA)
   If wb
     If wb\QueryInterface(?IID_IOleObject, @oObj) = #S_OK
        If oObj\QueryInterface(?IID_IOleInPlaceObject, @ipObj) = #S_OK
           ;Use MoveWindow() or ResizeGadget()
            MoveWindow_(hwGd, x, y, width, height, redraw)
            ;ResizeGadget(gd, x, y, width, height) maybe this calls SetObjectRects() again ?
            GetClientRect_(hwGd, @rc)
            ipObj\SetObjectRects(@rc, @rc)
            ipObj\Release()
         EndIf
         oObj\Release()
     EndIf
   EndIf
EndProcedure

;------------------------------------------------------------------------------
;- * Main program

Enumeration
  #Window_Main
EndEnumeration

Enumeration
  #Button_Start
  #Web_0
EndEnumeration

Define.IOleObject oleObject
Define.IOleControl oleControl
Define.i Event, WindowID, GadgetID, EventType

If OpenWindow(#Window_Main, 302, 15, 800, 620, "pb-klicker",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_SizeGadget )
  ButtonGadget(#Button_Start, 10, 570, 110, 40, "Start")
 
  WebGadget(#Web_0, 10, 10, 780, 550, "about:blank")
  ; WebGadget initialization
  Global myBrowser.IWebBrowser2 = GetWindowLongPtr_(GadgetID(#Web_0), #GWLP_USERDATA)
  If myBrowser\QueryInterface(?IID_IOleObject, @oleObject) = #S_OK
    ; new IDispatch object
    AddElement(g_IDispatch_Objects())
    g_IDispatch_Objects()\IDispatch = ?_IDispatch_Functions
    ; new IOleClientSite object
    AddElement(g_IOleClientSite_Objects())
    g_IOleClientSite_Objects()\IOleClientSite = ?_IOleClientSite_Functions
    ; tell the webbrowser client about our IOleClientSite object
    If oleObject\SetClientSite(@g_IOleClientSite_Objects()) = #S_OK
      If myBrowser\QueryInterface(?IID_IOleControl, @oleControl) = #S_OK
        ; tell the webbrowser control that Ambient Properties have changed so it queries our IOleClientSite object for the IDispatch interface and calls IDispatch::Invoke with DISPID_AMBIENT_DLCONTROL
        oleControl\OnAmbientPropertyChange(#DISPID_AMBIENT_DLCONTROL)
        oleControl\Release()
      EndIf
    EndIf
    oleObject\Release()
  EndIf
EndIf

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      ;ShellExecute_(#Null, @"open", @"c:\users\mmras\NetDisabler_x64.exe", @" /E", @"C:\users\mmras\", #SW_NORMAL)
      Quit = 1
      
    Case #PB_Event_SizeWindow
      ResizeWebgadget(#Web_0,10,10,WindowWidth(#Window_Main)-20,WindowHeight(#Window_Main)-70)
      ResizeGadget(#Button_Start,10,WindowHeight(#Window_Main)-50,110, 40)
 
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_Start
          ;ShellExecute_(#Null, @"open", @"c:\users\mmras\NetDisabler_x64.exe", @" /D", @"C:\users\mmras\", #SW_NORMAL)
          ;Delay(2000)
          Filename.S = "webpage"
          If ReadFile(0, GetHomeDirectory()+Filename+".html")
            While Eof(0) = 0
              Text.S = Text.S + ReadString(0)
            Wend
            CloseFile(0)
            Text = ReplaceString(Text, "href", "y")
            Text = ReplaceString(Text, "src", "x")
            Text = ReplaceString(Text, "http://", "")
            Text = ReplaceString(Text, "https://", "")
            Text = ReplaceString(Text, "ftp://", "")
            Text = ReplaceString(Text, "sftp://", "")
            Text = ReplaceString(Text, "smtp://", "")
            Text = ReplaceString(Text, "mailto:", "")
            If CreateFile(0, GetHomeDirectory()+Filename+"2.html")
              WriteStringN(0, Text)
              CloseFile(0)
            EndIf
          EndIf
          web$ = GetHomeDirectory()+Filename+"2.html"
          SetGadgetText(#Web_0, web$)  ;;  full path to local html file
      EndSelect
  EndSelect
Until Quit = 1

End

;- Data section

DataSection
  _IOleClientSite_Functions:
  Data.i @IOleClientSite_QueryInterface()
  Data.i @IOleClientSite_AddRef()
  Data.i @IOleClientSite_Release()
  Data.i @IOleClientSite_SaveObject()
  Data.i @IOleClientSite_GetMoniker()
  Data.i @IOleClientSite_GetContainer()
  Data.i @IOleClientSite_ShowObject()
  Data.i @IOleClientSite_OnShowWindow()
  Data.i @IOleClientSite_RequestNewObjectLayout()
 
  ; IOleClientSite
  ; {00000118-0000-0000-C000-000000000046}
  IID_IOleClientSite:
  Data.l $00000118
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  
  ; IOleObject
  ; {00000112-0000-0000-C000-000000000046}
  IID_IOleObject:
  Data.l $00000112
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46   
 
  ; IOleControl
  ; {B196B288-BAB4-101A-B69C-00AA00341D07}
  IID_IOleControl:
  Data.l $B196B288
  Data.w $BAB4, $101A
  Data.b $B6, $9C, $00, $AA, $00, $34, $1D, $07   

  ;("00000113-0000-0000-C000-000000000046")
  IID_IOleInPlaceObject:
  Data.l $00000113
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
  
  _IDispatch_Functions:
  Data.i @IDispatch_QueryInterface()
  Data.i @IDispatch_AddRef()
  Data.i @IDispatch_Release()
  Data.i @IDispatch_GetTypeInfoCount()
  Data.i @IDispatch_GetTypeInfo()
  Data.i @IDispatch_GetIDsOfNames()
  Data.i @IDispatch_Invoke()
 
  IID_IUnknown: ; {00000000-0000-0000-C000-000000000046}
  Data.l $00000000
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
 
  IID_IDispatch: ; {00020400-0000-0000-C000-000000000046}
  Data.l $00020400
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46
 
EndDataSection 
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Stop WebGadget accessing the internet

Post by IdeasVacuum »

Hi Rashad

Your average HTML page will exceed the max chars (8192) of a string. Better to:
1)Read the file (ReadString) line-by-line
2)Parse each line and remove css, image, script tags
3)Write the results to a new file for loading.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Stop WebGadget accessing the internet

Post by Marc56us »

Code: Select all

Your average HTML page will exceed the max chars (8192) of a string.
:?: A string size is illimited in PB (PB use Null-terminated string)
https://www.purebasic.com/documentation ... ables.html

:!: And be carefull, a script can be spread over several lines, so you have to read the whole page to find the closing tag before filtering. Especially if you remove only the opening tag of a script, the page will then display the whole script. :o

:wink: I had already proposed a filtering version to a file 10 days ago...
viewtopic.php?p=556009#p556009
:arrow: Here is an even more simplified version based on HTTPRequestMemory() instead of ReceiveHTTPMemory()
( :) Since the data on the web page is text, you can use HTTPRequest() as well as HTTPRequestMemory())

Code: Select all

; Show page in Webgadget without (some) links
; Marc56us - 2020/06/22
; A proposed solution for
; https://www.purebasic.fr/english/viewtopic.php?f=13&t=75430

OpenWindow(0,0,0,900,700,"",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
URL$ = "https://www.purebasic.fr/english/viewtopic.php?f=13&t=75430&p=556006"

InitNetwork()

HttpRequest = HTTPRequestMemory(#PB_HTTP_Get, URL$)
If HttpRequest 
    Source$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
    FinishHTTP(HttpRequest)
Else
    MessageRequester("Error", "Can't download page")
    End
EndIf

If Not CreateRegularExpression(0, ~"(?:href|src)=\"(.+?)\"")
    Debug "RegEx KO"
    End
EndIf

Source_Filtered$ = ReplaceRegularExpression(0, Source$, "")

WebGadget(0,10,10,880,680,"")
SetGadgetItemText(0, #PB_Web_HtmlCode, Source_Filtered$)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
:idea: Just add in the regular expression the other tags to filter (ie: <script> </script>)
But it's not always that simple because "modern" web pages are often scripting horrors :o :shock: :?

:wink:
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Stop WebGadget accessing the internet

Post by IdeasVacuum »

String Size - I see, it is actually an IDE limit but the error message does not suggest that! :shock:
viewtopic.php?f=13&t=69812
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply