-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix turn off issue by using style tag instead of inject css
- Loading branch information
1 parent
19e6144
commit b5ddf55
Showing
6 changed files
with
108 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const isOnStorageKey: string = 'isOn'; | ||
|
||
interface Style { | ||
textColor: string; | ||
backgroundColor: string; | ||
} | ||
|
||
const styleStorageKey: string = 'style'; | ||
|
||
const defaultStyle: Style = { | ||
textColor: 'rgba(255, 255, 255, 1)', | ||
backgroundColor: 'rgba(0, 0, 0, 0.5)', | ||
}; | ||
|
||
function applyStyle(style: Style) { | ||
const css = ` | ||
.player-timedtext > .player-timedtext-text-container > span > span { | ||
color: ${style.textColor} !important; | ||
background-color: ${style.backgroundColor} !important; | ||
} | ||
`; | ||
|
||
const currentStyleTag = document.querySelector('style#sub-stylist-style') as HTMLStyleElement | null; | ||
if (currentStyleTag) { | ||
currentStyleTag.textContent = css; | ||
return; | ||
} | ||
|
||
const styleTag = document.createElement('style'); | ||
styleTag.id = 'sub-stylist-style'; | ||
styleTag.textContent = css; | ||
document.head.appendChild(styleTag); | ||
} | ||
|
||
function removeStyle() { | ||
const currentStyleTag = document.querySelector('style#sub-stylist-style') as HTMLStyleElement | null; | ||
if (currentStyleTag) { | ||
currentStyleTag.remove(); | ||
} | ||
} | ||
|
||
async function loadAndApplyStyle() { | ||
const states = await chrome.storage.sync.get([isOnStorageKey, styleStorageKey]); | ||
const isOn = states[isOnStorageKey] as boolean | undefined; | ||
if (!isOn) { | ||
return; | ||
} | ||
|
||
const style = states[styleStorageKey] as Style | undefined; | ||
if (style !== undefined) { | ||
applyStyle(style); | ||
} else { | ||
applyStyle(defaultStyle); | ||
} | ||
} | ||
|
||
chrome.runtime.onMessage.addListener((message) => { | ||
if (message.action === 'changeStyle') { | ||
const isOn = message.payload.isOn as boolean; | ||
if (isOn) { | ||
const style = message.payload.style as Style; | ||
applyStyle(style); | ||
} else { | ||
removeStyle(); | ||
} | ||
} | ||
}); | ||
|
||
loadAndApplyStyle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +0,0 @@ | ||
import { applyStyle, defaultStyle, isOnSchema, isOnStorageKey, styleSchema, styleStorageKey } from './modules'; | ||
|
||
async function loadAndApplyStyle() { | ||
const states = await chrome.storage.sync.get([isOnStorageKey, styleStorageKey]); | ||
const isOn = isOnSchema.optional().parse(states[isOnStorageKey]); | ||
if (!isOn) { | ||
console.warn('now off'); | ||
return; | ||
} | ||
|
||
const style = styleSchema.optional().parse(states[styleStorageKey]); | ||
if (style !== undefined) { | ||
await applyStyle(style); | ||
} else { | ||
await applyStyle(defaultStyle); | ||
} | ||
} | ||
|
||
chrome.tabs.onUpdated.addListener(() => { | ||
loadAndApplyStyle(); | ||
}); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters