Is it possible to set the webkit user agent?

Mac OSX specific forum
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Is it possible to set the webkit user agent?

Post by infratec »

Hi,

we need to set the useragent which is used by the webgadget (webkit).
Is there any chance to do this from PB?

Code: Select all

If OpenWindow(0, 0, 0, 900, 500, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  WebGadget(0, 0, 0, 900, 500, "http://wieistmeinuseragent.de") 
  
  
  AddWindowTimer(0, 0, 1000)
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Timer
      ;Debug "Do it now"
      RemoveWindowTimer(0, 0)
      
      ; do some magic to change the user agent
  
      ;SetGadgetState(0, #PB_Web_Refresh)
      SetGadgetText(0, "http://wieistmeinuseragent.de")
    EndIf
    
  Until Event = #PB_Event_CloseWindow 
EndIf
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to set the webkit user agent?

Post by infratec »

Maybe like this:

Code: Select all

NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : "Custom Agent"])
And how to do this in PB?

Code: Select all

standardUserDefaults = CocoaMessage(0, 0, "NSUserDefaults standardUserDefaults")
CocoaMessage(0, standardUserDefaults, "setObject:", CocoaMessage(0,0,"NSString:", @"Custom Agent"), "forKey:$", @"UserAgent")

Code: Select all

standardUserDefaults = CocoaMessage(0, 0, "NSUserDefaults standardUserDefaults")
CocoaMessage(0, standardUserDefaults, "setObject:$", @"Custom Agent", "forKey:$", @"UserAgent")
:?:
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is it possible to set the webkit user agent?

Post by mk-soft »

You can use set custom user agent for web view. With set NIL remove custom user agent

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainWebView
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure.s CocoaString(NSString)
  Protected r1.s
  If NSString
    r1 = PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
  EndIf
  ProcedureReturn r1
EndProcedure

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainWebView, 0, 0, dx, dy)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    WebGadget(#MainWebView, 0, 0, dx, dy, "")
    
    CocoaMessage(0, GadgetID(#MainWebView), "setCustomUserAgent:$", @"custom user agent")
    
    NSString = CocoaMessage(0, GadgetID(#MainWebView), "customUserAgent")
    StatusBarText(#MainStatusBar, 0, " UserAgent: " + CocoaString(NSString))
    
    SetGadgetText(0, "http://wieistmeinuseragent.de")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to set the webkit user agent?

Post by infratec »

Thank you mk-soft,

Code: Select all

CocoaMessage(0, GadgetID(#MainWebView), "setCustomUserAgent:$", @"custom user agent")
Works very well.

The original webgadget useragent is missing the Safari part at the end.
So it was not possible to start https://test.bigbluebutton.org

Now it works :wink:

Code: Select all

Procedure SizeWindowHandler()   
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0))
EndProcedure


If OpenWindow(0, 0, 0, 1200, 768, "WebGadget", #PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered) 
  WebGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), "https://test.bigbluebutton.org")
  BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler())
  
  AddWindowTimer(0, 0, 1000)
  Repeat
    Event = WaitWindowEvent()
    Select Event 
      Case #PB_Event_Timer
        RemoveWindowTimer(0, 0)
        
        CocoaMessage(0, GadgetID(0), "setCustomUserAgent:$", @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15")
        
        SetGadgetState(0, #PB_Web_Refresh)
    EndSelect
    
  Until Event = #PB_Event_CloseWindow 
EndIf
Post Reply