From 7ae3e0ddccef266284e24cf117eabcedcfe6e04a Mon Sep 17 00:00:00 2001 From: Hakadao Date: Wed, 6 Sep 2023 14:39:28 +0800 Subject: [PATCH] refactor: adjust `injectApp()` function --- src/contentScripts/index.ts | 54 +++++++++++++------------------------ 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/contentScripts/index.ts b/src/contentScripts/index.ts index b453e824..ef0fdf79 100644 --- a/src/contentScripts/index.ts +++ b/src/contentScripts/index.ts @@ -45,40 +45,15 @@ function injectApp() { const currentUrl = document.URL if ( + // homepage /https?:\/\/bilibili.com\/?$/.test(currentUrl) - || /https?:\/\/www.bilibili.com\/?$/.test(currentUrl) - || /https?:\/\/www.bilibili.com\/index.html$/.test(currentUrl) - || /https?:\/\/bilibili.com\/\?spm_id_from=.*/.test(currentUrl) - || /https?:\/\/www.bilibili.com\/\?spm_id_from=(.)*/.test(currentUrl) - ) { - const originalPageContent = document.querySelector('#i_cecream') - if (originalPageContent) - originalPageContent.innerHTML = '' + || /https?:\/\/www.bilibili.com\/?$/.test(currentUrl) + || /https?:\/\/www.bilibili.com\/index.html$/.test(currentUrl) + || /https?:\/\/bilibili.com\/\?spm_id_from=.*/.test(currentUrl) + || /https?:\/\/www.bilibili.com\/\?spm_id_from=(.)*/.test(currentUrl) - // mount component to context window - const container = document.createElement('div') - container.id = 'bewly' - const root = document.createElement('div') - const styleEl = document.createElement('link') - const shadowDOM = container.attachShadow?.({ mode: __DEV__ ? 'open' : 'closed' }) || container - styleEl.setAttribute('rel', 'stylesheet') - styleEl.setAttribute('href', browser.runtime.getURL('dist/contentScripts/style.css')) - shadowDOM.appendChild(styleEl) - shadowDOM.appendChild(root) - - // inject svg icons - const svgDiv = document.createElement('div') - svgDiv.innerHTML = SVG_ICONS - shadowDOM.appendChild(svgDiv) - - document.body.appendChild(container) - app = createApp(App) - setupApp(app) - app.use(i18n).mount(root) - } - else if ( // video page - /https?:\/\/(www.)?bilibili.com\/video\/.*/.test(currentUrl) + || /https?:\/\/(www.)?bilibili.com\/video\/.*/.test(currentUrl) // watch later playlist || /https?:\/\/(www.)?bilibili.com\/list\/watchlater.*/.test(currentUrl) // favorite playlist @@ -88,9 +63,17 @@ function injectApp() { // moments || /https?:\/\/t.bilibili.com\.*/.test(currentUrl) ) { - // const originalPageContent = document.querySelector('#i_cecream') - // if (originalPageContent) - // originalPageContent.innerHTML = '' + if ( + /https?:\/\/bilibili.com\/?$/.test(currentUrl) + || /https?:\/\/www.bilibili.com\/?$/.test(currentUrl) + || /https?:\/\/www.bilibili.com\/index.html$/.test(currentUrl) + || /https?:\/\/bilibili.com\/\?spm_id_from=.*/.test(currentUrl) + || /https?:\/\/www.bilibili.com\/\?spm_id_from=(.)*/.test(currentUrl) + ) { + const originalPageContent = document.querySelector('#i_cecream') + if (originalPageContent) + originalPageContent.innerHTML = '' + } // mount component to context window const container = document.createElement('div') @@ -101,14 +84,13 @@ function injectApp() { styleEl.setAttribute('rel', 'stylesheet') styleEl.setAttribute('href', browser.runtime.getURL('dist/contentScripts/style.css')) shadowDOM.appendChild(styleEl) + shadowDOM.appendChild(root) const newStyleEl = document.createElement('link') newStyleEl.setAttribute('rel', 'stylesheet') newStyleEl.setAttribute('href', browser.runtime.getURL('dist/contentScripts/style.css')) document.body.appendChild(newStyleEl) - shadowDOM.appendChild(root) - // inject svg icons const svgDiv = document.createElement('div') svgDiv.innerHTML = SVG_ICONS