I'm currently working on developing a Safari extension that allows users to share screenshots of webpages. However, I've encountered an issue when trying to pass the image back to Swift - it triggers an error that causes Safari to become unstable and abruptly terminates my task midway through the process.
The concept behind this extension is that when a user clicks on a toolbar button, any selected text and a screenshot of the webpage are saved. I'm attempting to pass both of these pieces of data through the userInfo dictionary. When I run my code with the dispatchMessage call commented out, I don't encounter any errors. But if I uncomment the dispatch call, I receive the following error message:
WebKitSubtleCrypto is deprecated. Please use SubtleCrypto instead.
Below is my JavaScript code:
document.addEventListener("DOMContentLoaded", function(event) {
safari.self.addEventListener("message", handleMessage);
});
function handleMessage(event) {
var selectedText = window.getSelection().toString();
var screenshot;
if (window.top === window) {
html2canvas(document.getElementsByTagName('html')).then(function(canvas) {
screenshot = convertCanvasToImage(canvas);
console.log("canvas image: " + screenshot)
safari.extension.dispatchMessage("test", {"selectedText": selectedText, "screenshot" : canvas});
});
}
}
function convertCanvasToImage(canvas) {
var image = new Image();
image.src = canvas.toDataURL("image/png");
return image;
}
The html2canvas library (latest version - 0.5.0-beta4) is included in another file packaged with the extension.
Edit 1
After conducting further tests, it appears that this error is related solely to passing the 'screenshot' object in the messageDispatch call. If I remove the screenshot and only pass the selectedText data, everything functions as expected. Additionally, I've attempted to pass the screenshot as a canvas without running it through the 'convertCanvasToImage()' function, but I continue to encounter the same error.