receiving messages from webView

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







An update to this,

I had no luck with finding a specific way to handle these notifications, so i fudged it

here is what i did

whenever i need to send a notification, I will change the title from javascript, and prefix it with “Notification:”

then do the following




property LastTitle : ""

on webView:webView didReceiveTitle:aTitle forFrame:frame
	log "TITLE RECEIVED, Checking if it is a notification"
	if (aTitle as string) contains "Notification:" then
		log "A notification has come through"
		set theNotification to aTitle
		handleNotification(theNotification)
	else
		set LastTitle to aTitle
		
	end if
	--Set the title back to what it was, or keep it if it is a valid title 
	doJavaScript("document.title = '" & LastTitle & "' ")
	
	
end webView:didReceiveTitle:forFrame: