Skip to content

Commit

Permalink
<generate>[feat]add argument to custom js
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkar-Suki committed Aug 15, 2024
1 parent c1c5a31 commit 0215166
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
31 changes: 23 additions & 8 deletions src/plugins/jx3/announce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@

import os
import re
import shutil

js_content = f"""
const style = document.createElement('style');
style.textContent = `
@font-face {{
font-family: 'Custom';
src: url('$font_path') format('ttf');
}}
body {{
font-family: 'Custom', sans-serif !important;
}}
`;
document.head.appendChild(style);
"""

announce = on_command("jx3_announce", aliases={"维护公告", "更新公告", "公告", "更新"}, force_whitespace=True, priority=5)

Expand All @@ -28,24 +42,25 @@ async def _(event: GroupMessageEvent, args: Message = CommandArg()):
return
if os.path.exists(ASSETS + "/jx3/update.png"):
img = get_content_local(Path(ASSETS + "/jx3/update.png").as_uri())
await announce.finish(ms.image(ms.image(img)))
await announce.finish(ms.image(img))
else:
data = await get_api(f"{Config.jx3.api.url}/data/news/allnews")
for news in data["data"]:
title = news["title"]
url = news["url"]
if re.match(r'(\d+)月(\d+)日(.*?)版本更新公告', title):
shutil.rmtree(ASSETS + "/jx3/update.png")
await generate(
final_js = js_content.replace("$font_path", Path(ASSETS + "/font/syst-mid.ttf").as_uri())
print(final_js)
store_path = await generate(
url,
True,
".allnews_list_container",
viewport={"height": 3840, "width": 2000},
hide_classes=["detail_bot", "bdshare-slide-button"],
device_scale_factor=2.0,
additional_js=final_js,
output=ASSETS + "/jx3/update.png"
)
break
if os.path.exists(ASSETS + "/jx3/update.png"):
img = get_content_local(Path(ASSETS + "/jx3/update.png").as_uri())
await announce.finish(ms.image(ms.image(img)))
img = get_content_local(Path(store_path).as_uri())
await announce.finish(ms.image(img))

21 changes: 14 additions & 7 deletions src/tools/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(
first: bool = False,
delay: int = 0,
additional_css: str = "",
additional_js: str = "",
viewport: dict = None,
full_screen: bool = False,
hide_classes: list = [],
Expand All @@ -29,7 +30,8 @@ def __init__(
self.locate = locate
self.first = first
self.delay = delay
self.additional_css = additional_css
self.additional_css = additional_css or ""
self.additional_js = additional_js or ""
self.viewport = viewport or {}
self.full_screen = full_screen
self.hide_classes = hide_classes
Expand All @@ -50,7 +52,11 @@ async def _apply_customizations(self, page):
if self.delay > 0:
await asyncio.sleep(self.delay / 1000)
if self.web:
await page.add_style_tag(content=self.additional_css)
if self.additional_css:
print(1)
await page.add_style_tag(content=self.additional_css)
if self.additional_js:
await page.evaluate(self.additional_js)
if self.hide_classes:
await self._hide_elements_by_class(page)

Expand All @@ -67,8 +73,8 @@ async def _capture_element_screenshot(self, locator, store_path):
async def _capture_full_screenshot(self, page, store_path):
await page.screenshot(path=store_path, full_page=self.full_screen)

async def _save_screenshot(self, page, store_path=None):
store_path = f"{CACHE}/{self.uuid}.png" if store_path is None else store_path
async def _save_screenshot(self, page, store_path):
store_path = f"{CACHE}/{self.uuid}.png" if not store_path else store_path
if self.locate:
locator = page.locator(self.locate)
await self._capture_element_screenshot(locator, store_path)
Expand All @@ -92,7 +98,7 @@ async def generate(self):
return store_path

except Exception as ex:
logger.error(f"图片生成失败,请尝试执行 `playwright install`!: {ex}")
print(f"图片生成失败,请尝试执行 `playwright install`!: {ex}")
return False

async def generate(
Expand All @@ -102,11 +108,12 @@ async def generate(
first: bool = False,
delay: int = 0,
additional_css: str = "",
additional_js: str = "",
viewport: dict = None,
full_screen: bool = False,
hide_classes: list = None,
device_scale_factor: float = 1.0,
output: str = None
output: str = ""
):
generator = ScreenshotGenerator(path, web, locate, first, delay, additional_css, viewport, full_screen, hide_classes, device_scale_factor, output)
generator = ScreenshotGenerator(path, web, locate, first, delay, additional_css, additional_js, viewport, full_screen, hide_classes, device_scale_factor, output)
return await generator.generate()

0 comments on commit 0215166

Please sign in to comment.