fix: insertCSS not working in mv3

This commit is contained in:
Hakadao
2022-10-23 01:02:01 +08:00
parent 7f57b812a0
commit 74144ed331
2 changed files with 20 additions and 19 deletions

View File

@@ -52,10 +52,12 @@ onMessage('get-current-tab', async () => {
// preinsert css
browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChangeInfoType, tab: Tabs.Tab) => {
if (/https?:\/\/bilibili.com\/?$/.test(`${tab.url}`)
|| /https?:\/\/www.bilibili.com\/?$/.test(`${tab.url}`)
|| /https?:\/\/bilibili.com\/\?spm_id_from=.*/.test(`${tab.url}`)
|| /https?:\/\/www.bilibili.com\/\?spm_id_from=(.)*/.test(`${tab.url}`)) {
if (
/https?:\/\/bilibili.com\/?$/.test(`${tab.url}`)
|| /https?:\/\/www.bilibili.com\/?$/.test(`${tab.url}`)
|| /https?:\/\/bilibili.com\/\?spm_id_from=.*/.test(`${tab.url}`)
|| /https?:\/\/www.bilibili.com\/\?spm_id_from=(.)*/.test(`${tab.url}`)
) {
if (changInfo.status === 'loading') {
const css = `
body {
@@ -66,16 +68,16 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan
}
`
browser.scripting.insertCSS(tabId, {
css: css,
runAt: 'document_start',
target: {tabId: tabId},
matchAboutBlank: true,
browser.scripting.insertCSS({
css,
target: {
tabId,
},
})
// If it not a macOS, we will inject CSS to design the scrollbar
if (!navigator.userAgent.includes('Mac OS X')) {
browser.scripting.insertCSS(tabId, {
browser.scripting.insertCSS({
css: `
::-webkit-scrollbar {
width: 8px;
@@ -94,13 +96,12 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan
border-radius: 20px;
}
`,
runAt: 'document_start',
target: {tabId: tabId},
matchAboutBlank: true,
// runAt: 'document_start',
target: { tabId },
// matchAboutBlank: true,
})
}
}
else if (changInfo.status === 'complete') {
const css = `
body {
@@ -110,11 +111,9 @@ browser.tabs.onUpdated.addListener((tabId: number, changInfo: Tabs.OnUpdatedChan
}
`
browser.scripting.insertCSS(tabId, {
css: css,
runAt: 'document_start',
target: {tabId: tabId},
matchAboutBlank: true,
browser.scripting.insertCSS({
css,
target: { tabId },
})
}
}

View File

@@ -35,6 +35,8 @@ export async function getManifest() {
{
matches: ['http://www.bilibili.com/*', 'https://www.bilibili.com/*'],
js: ['./dist/contentScripts/index.global.js'],
run_at: 'document_start',
match_about_blank: true,
},
],
web_accessible_resources: [