Skip to content

Commit f4ef974

Browse files
committed
fix(bilibili): dynamic font
1 parent ca178be commit f4ef974

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/plugins/bilibili/plugins/dynamic/__init__.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from nonebot.plugin import PluginMetadata
1212
from nonebot_plugin_alconna import Alconna, Image, UniMessage, on_alconna
1313
from nonebot_plugin_apscheduler import scheduler
14-
from nonebot_plugin_htmlrender import get_browser, get_new_page
14+
from nonebot_plugin_htmlrender import get_browser
1515
from nonebot_plugin_orm import async_scoped_session, get_session
1616
from nonebot_plugin_session import EventSession
1717
from nonebot_plugin_session_orm import get_session_persist_id
@@ -75,15 +75,24 @@ def replace(self, item: str) -> str | None:
7575
cache = Cache()
7676

7777

78+
_context: BrowserContext | None = None
79+
7880
async def get_context() -> BrowserContext:
79-
context = await (await get_browser()).new_context(**plugin_config.screenshot_device)
80-
await context.add_cookies(
81+
global _context
82+
83+
if _context and _context.browser and _context.browser.is_connected():
84+
return _context
85+
86+
_context = await (await get_browser()).new_context(
87+
**plugin_config.screenshot_device
88+
)
89+
await _context.add_cookies(
8190
[
8291
{"name": name, "value": value, "domain": ".bilibili.com", "path": "/"}
8392
for name, value in bilibili_config.cookies.items()
8493
]
8594
)
86-
await context.add_cookies(
95+
await _context.add_cookies(
8796
[
8897
{
8998
"name": "SESSDATA",
@@ -95,12 +104,24 @@ async def get_context() -> BrowserContext:
95104
}
96105
]
97106
)
98-
return context
107+
108+
pattern = "@540w_540h_1c.webp"
109+
await _context.route(
110+
"**/*" + pattern,
111+
lambda route: route.continue_(
112+
url=route.request.url[: -len(pattern)] + "@540w_540h_1c_!header.webp"
113+
),
114+
)
115+
116+
return _context
99117

100118

101119
@asynccontextmanager
102120
async def get_new_page(**kwargs) -> AsyncGenerator[Page, Any]:
103-
async with await get_context() as context, await context.new_page(**kwargs) as page:
121+
async with await (await get_context()).new_page(**kwargs) as page:
122+
await (await page.context.new_cdp_session(page)).send(
123+
"Network.setCacheDisabled", {"cacheDisabled": False}
124+
)
104125
yield page
105126

106127

@@ -181,7 +202,6 @@ async def render_screenshot(id_str: str) -> bytes:
181202
await page.wait_for_load_state("domcontentloaded")
182203
await page.add_style_tag(
183204
content="""
184-
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Symbols+2&family=Noto+Sans:ital,wght@0,100..900;1,100..900&display=swap');
185205
@font-face {
186206
font-family: "LXGW ZhenKai";
187207
src: url("https://hanabi-live.com/_static/assets/f167150329dc0576d84f4d14b65adbd1.ttf") format("truetype");
@@ -190,12 +210,8 @@ async def render_screenshot(id_str: str) -> bytes:
190210
font-family: "LXGW WenKai";
191211
src: url("https://hanabi-live.com/_static/assets/174d37b738e6c3935c301fb01d1ec695.ttf") format("truetype");
192212
}
193-
@font-face {
194-
font-family: "Segoe UI Emoji";
195-
src: url("https://hanabi-live.com/_static/assets/aa786a4b6a6ae2a1f7b9e87d281be8cf.ttf") format("truetype");
196-
}
197213
* {
198-
font-family: "LXGW ZhenKai", "LXGW WenKai", "Noto Sans", "Noto Sans Symbols 2", "Segoe UI Emoji" !important;
214+
font-family: "LXGW ZhenKai", "LXGW WenKai", sans-serif !important;
199215
}
200216
"""
201217
)

0 commit comments

Comments
 (0)