Here are the steps to get the screenshot of the web page
1) send a message from the injected script to the app extension using safari.extension.disptachMessage
2) Below method in SafariExtensionHandler class will be invoked.
func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?)
3) screenshot can be taken using "page" parameter in the above mentioned function.
page.getScreenshotOfVisibleArea { (NSImage)
}
NSImage is the screenshot of the web page
Remember you can only get screenshot of currently visible area of the web page not the whole page.