Following the discussion in Context menu for HtmlViewer I need to find out if the mouse is over an an image in the HtmlViewer (or WKWebviewMBS) or not.
You’ll add script to your web page with event listeners for mouseenter and mouseleave like so:
// Getting the image element by its ID
const image = document.getElementById('specialImage');
// Adding event listeners for mouseenter and mouseleave
image.addEventListener('mouseenter', function() {
executeInXojo('mouseenter', 'specialImage');
});
image.addEventListener('mouseleave', function() {
executeInXojo('mouseexit', 'specialImage');
});
Then in Xojo, go to your HTMLViewer and handle the JavascriptRequest event with the following code:
Function JavaScriptRequest(method As String, parameters() as Variant) Handles JavaScriptRequest as String
select case method
case "mouseenter"
if parameters.Count = 0 then return
select case parameters(0).StringValue
case "specialImage"
//Mouse is over the image
end select
case "mouseexit"
if parameters.Count = 0 then return
select case parameters(0).StringValue
case "specialImage"
//Mouse has left the image
end select
end select
End Function
<a id="myLink" href="max-attachment-handler://ha_stat_19_20190131_235301_346525385.png">This is the link text, you can optionally put an img tag here too</a>
...
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const link = document.getElementById('myLink');
// Mouse enter event
link.addEventListener('mouseenter', function() {
executeInXojo('mouseenter', 'myLink');
});
// Mouse leave event
link.addEventListener('mouseleave', function() {
executeInXojo('mouseexit', 'myLink');
});
});
</script>
If you want this to be a bit more dynamic, I’ve modified Christian’s example. The JavaScript code will now attach an event handler to all links on the page that contain an IMG element, it’s executed from DocumentComplete so you don’t need to modify the HTML you’re loading before-hand (in case that’s an issue), and the JavaScript returns the href value of the link to JavaScriptRequest so you can differentiate multiple elements.