Search code examples
safariscreenshotsafari-extension

Safari Extension Taking Screenshot


I am developing safari browser extension which should have taking screenshot functionality.

Chrome and Firefox has their own apis to take SS of the current window document. I could not find any safari specific/API documentation for it. The Windows and Tabs API

What would be the best way to achieve it?


Solution

  • SafariBrowserTab has a visibleContentsAsDataURL method to get the image data of the currently visible content.

    For example, in your global page:

    safari.application.addEventListener('command', performCommand, false);
    
    // Perform e.g. when toolbar button is clicked
    function performCommand(event) {
        if (event.command === 'captureTab') {
            var tab = safari.application.activeBrowserWindow.activeTab;
            tab.visibleContentsAsDataURL(function(imgdata) {
                //console.log(imgdata);
                // Do something...
                // e.g. Send to an injected script to display image on page:
                tab.page.dispatchMessage('imgData', imgdata);
            });
        }
    }