From 88cf0b583e5e2438b0f35361cd3068e9b9d6ac25 Mon Sep 17 00:00:00 2001 From: domsgit Date: Sat, 20 May 2023 01:58:22 +0800 Subject: [PATCH] fix: issue#5 --- manifest.json | 2 +- src/chrome/content-script.cjs | 29 +++++++++++++++++++---------- src/const.tsx | 2 ++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/manifest.json b/manifest.json index a4cdba2..871543b 100644 --- a/manifest.json +++ b/manifest.json @@ -11,7 +11,7 @@ }, "content_scripts": [ { - "matches": ["https://www.bilibili.com/video/*"], + "matches": ["https://www.bilibili.com/video/*", "https://www.bilibili.com/list/watchlater*"], "js": ["src/chrome/content-script.cjs"] } ], diff --git a/src/chrome/content-script.cjs b/src/chrome/content-script.cjs index 47d5840..35cb903 100644 --- a/src/chrome/content-script.cjs +++ b/src/chrome/content-script.cjs @@ -1,4 +1,4 @@ -const {TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX} = require("../const"); +const {TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX, IFRAME_ID} = require("../const"); var totalHeight = TOTAL_HEIGHT_DEF const getVideoElement = () => { @@ -23,7 +23,7 @@ setInterval(function () { } const iframe = document.createElement('iframe') - iframe.id = 'bilibili-subtitle-iframe' + iframe.id = IFRAME_ID iframe.src = chrome.runtime.getURL('index.html') iframe.style = 'border: none; width: 100%; height: 44px;' iframe.allow = 'clipboard-read; clipboard-write;' @@ -43,15 +43,24 @@ let pagesMap = {} let lastAidOrBvid = null const refreshVideoInfo = async () => { - const iframe = document.getElementById('bilibili-subtitle-iframe') + const iframe = document.getElementById(IFRAME_ID) if (!iframe) return - let path = location.pathname - if (path.endsWith('/')) { - path = path.slice(0, -1) + // fix: https://github.com/IndieKKY/bilibili-subtitle/issues/5 + // 处理稍后再看的url( https://www.bilibili.com/list/watchlater?bvid=xxx&oid=xxx ) + const pathSearchs = {} + location.search.slice(1).replace(/([^=&]*)=([^=&]*)/g, (matchs, a, b, c) => pathSearchs[a] = b) + + // bvid + let aidOrBvid = pathSearchs.bvid // 默认为稍后再看 + if (!aidOrBvid) { + let path = location.pathname + if (path.endsWith('/')) { + path = path.slice(0, -1) + } + const paths = path.split('/') + aidOrBvid = paths[paths.length - 1] } - const paths = path.split('/') - const aidOrBvid = paths[paths.length - 1] if (aidOrBvid !== lastAidOrBvid) { // console.debug('refreshVideoInfo') @@ -100,7 +109,7 @@ const refreshVideoInfo = async () => { let lastAid = null let lastCid = null const refreshSubtitles = () => { - const iframe = document.getElementById('bilibili-subtitle-iframe') + const iframe = document.getElementById(IFRAME_ID) if (!iframe) return const urlSearchParams = new URLSearchParams(window.location.search) @@ -135,7 +144,7 @@ window.addEventListener("message", (event) => { const {data} = event if (data.type === 'fold') { - const iframe = document.getElementById('bilibili-subtitle-iframe') + const iframe = document.getElementById(IFRAME_ID) iframe.style.height = (data.fold ? HEADER_HEIGHT : totalHeight) + 'px' } diff --git a/src/const.tsx b/src/const.tsx index 0f3c739..7685303 100644 --- a/src/const.tsx +++ b/src/const.tsx @@ -1,5 +1,7 @@ export const APP_DOM_ID = 'bilibili-subtitle' +export const IFRAME_ID = 'bilibili-subtitle-iframe' + export const STORAGE_ENV = 'bilibili-subtitle_env' export const EVENT_EXPAND = 'expand'