Page 1 of 1
Is it possible to set the webkit user agent?
Posted: Thu Mar 02, 2023 2:11 pm
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
Re: Is it possible to set the webkit user agent?
Posted: Thu Mar 02, 2023 11:32 pm
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")

Re: Is it possible to set the webkit user agent?
Posted: Fri Mar 03, 2023 6:00 pm
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()
Re: Is it possible to set the webkit user agent?
Posted: Mon Mar 06, 2023 8:00 am
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
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