sparkles: add safari support.
# coding=utf-8
import base64
+import time
import os
from selenium import webdriver
@@ -13,28 +14,31 @@ def make_snapshot(
pixel_ratio: int = 2,
delay: int = 2,
is_remove_html: bool = True,
+ browser='Chrome'
):
- option = webdriver.ChromeOptions()
- option.add_argument("headless")
- capabilities = DesiredCapabilities.CHROME
- capabilities["loggingPrefs"] = {"browser": "ALL"}
- driver = webdriver.Chrome(options=option, desired_capabilities=capabilities)
+ if browser == 'Chrome':
+ driver = get_chrome()
+ elif browser == 'Safari':
+ driver = get_safari()
+ else:
+ raise Exception('Unknown browser!')
driver.set_script_timeout(delay + 1)
if not html_path.startswith("http"):
- html_path = os.path.abspath(html_path)
+ html_path = 'file://' + os.path.abspath(html_path)
driver.get(html_path)
+ time.sleep(delay)
ext = image_name.split(".")[1]
try:
- driver.execute_async_script(__gen_js_code(ext, pixel_ratio, delay))
+ output = driver.execute_script(__gen_js_code(ext, pixel_ratio, delay))
except exceptions.TimeoutException:
pass
try:
- output = driver.get_log("browser")[0]["message"]
- output = output.split(" ")[2].replace('"', "").split(",")[1]
+ print(output)
+ output = output.split(",")[1]
except:
raise
@@ -50,17 +54,27 @@ def make_snapshot(
def __gen_js_code(file_type: str, pixel_ratio: int, delay: int) -> str:
script = (
"""
- fn = function(){
var ele = document.querySelector('div[_echarts_instance_]');
var mychart = echarts.getInstanceByDom(ele);
return mychart.getDataURL(
{type:'--file-type--', pixelRatio: --pixel-ratio--, excludeComponents: ['toolbox']});
- };
- window.setTimeout(function() {var content = fn(); console.log(content);}, --delay--)
""".replace(
"--file-type--", file_type
)
.replace("--pixel-ratio--", str(pixel_ratio))
- .replace("--delay--", str(delay * 1000))
)
return script
+
+
+def get_chrome():
+ option = webdriver.ChromeOptions()
+ option.add_argument("headless")
+ capabilities = DesiredCapabilities.CHROME
+ capabilities["loggingPrefs"] = {"browser": "ALL"}
+ return webdriver.Chrome(
+ options=option,
+ desired_capabilities=capabilities)
+
+
+def get_safari():
+ return webdriver.Safari(executable_path='/usr/bin/safaridriver')
