Hi!
looking at webframeloaddelegate here. https://developer.apple.com/documentation/webkit/webframeloaddelegate
I have an application that sends an AJAX request repetitively and updates an object with the new data if it has changed,
i want to notify my application if the object meets certain criteria, and use that to send a usernotification.
Currently i can check if the object has changed using a timer, and looking regularly,
but is there a way to simply report back to my application instead of checking it with a timer
a basic idea of the code is here,
if there is no direct way to do this, I have read online that sometimes people use a redirect command and extract the parameters, then cancel it from your application, but haven’t had much success
In my interface builder i have declared webviewdelegate as an object, and set it as the frameloaddelegate for the webview
script AppDelegate
property NSTimer : class "NSTimer"
property theTimer : "PlaceHolder"
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--Interface Builder Outlets
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
property windowController : missing value
property webViewDelegate : missing value
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--Delegate Handlers
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
on applicationWillFinishLaunching:aNotification
log webViewDelegate's dothing()
set theTimer to NSTimer's scheduledTimerWithTimeInterval:1 target:me selector:"checkforchanges:" userInfo:"Nil" repeats:true
end applicationWillFinishLaunching:
on applicationShouldTerminate:sender
return current application's NSTerminateNow
end applicationShouldTerminate:
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--Main
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
on checkforchanges_()
set theNotificationQueue to webViewDelegate's doJavaScript("theNotificationQueue")
end checkforchanges_
end script
script webViewDelegate
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--Interface Builder Outlets
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
property webView : missing value
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--Delegate Handlers
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
on webView:webView didFinishLoadForFrame:webFrame --When a web page loads
log "Page is Loaded"
end webView:didFinishLoadForFrame:
on webView:webView didFailLoadWithError:theError forFrame:webFrame --When a web Page fails to load
log "Error Loading"
end webView:didFailLoadWithError:forFrame:
--What I'd Like
on webViewJustSetAJavaScriptVariableToWhatYouWereLookingFor:AJavascriptVariable
--Do my stuff
end webViewJustSetAJavaScriptVariableToWhatYouWereLookingFor:
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--Main
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
on dothing()
set JavaScriptFile to ("/applications/thisapp.app/lib/myjsfile.txt")
set thescript to read JavaScriptFile
doJavaScript(thescript)
end dothing
on doJavaScript(thescript)
try
set thereturn to webView's stringByEvaluatingJavaScriptFromString:thescript
if thereturn as string is "" then
set thereturn to false
end if
on error
set thereturn to false
end try
return thereturn as string
end doJavaScript
end script