Skip to content

Commit

Permalink
🚧 Fix mount twice issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
tolerious committed Sep 14, 2024
1 parent f320699 commit c7548b3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"content_script.js",
"assets/js/plyr.js",
"assets/js/youtube-transport.js"
],
"js": ["content_script.js", "assets/js/plyr.js"],
"css": ["assets/css/plyr.css"],
"run_at": "document_end"
}
Expand Down
28 changes: 23 additions & 5 deletions src/plugins/youtube/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ export function createYoutubeStylishReaderIcon() {
const toolBar = document.querySelector(".ytp-right-controls");
if (toolBar) {
const element = createYoutubeStylishIconElement();
element.addEventListener("click", onClickStylishReaderIcon);
logger("add icon...");
// element.addEventListener("click", onClickStylishReaderIcon);
toolBar.appendChild(element);
}
}

async function onClickStylishReaderIcon(e) {
logger(currentYoutubeTranslationUrl);
logger(`currentYoutubeTranslationUrl: ${currentYoutubeTranslationUrl}`);
logger(currentYoutubeZhTranslationUrl);
if (
currentYoutubeTranslationUrl.trim() === "" ||
Expand Down Expand Up @@ -146,6 +147,7 @@ export function parseSubtitles(url, data) {
}

function sendMessageToYoutubeVideoPage(message) {
logger("sendMessageToYoutubeVideoPage");
const event = new CustomEvent("fromYoutubeVideoContentScript", {
detail: JSON.stringify(message),
bubbles: true,
Expand Down Expand Up @@ -201,6 +203,7 @@ function injectJsToShadowDom(jsFileUrl) {
}

async function createYoutubeMiddlewareToShadowDom() {
logger("createYoutubeMiddlewareToShadowDom");
const shadowRoot = document.createElement("div");
shadowRoot.id = youtubeShadowRootId;
shadowRoot.style.display = "none";
Expand Down Expand Up @@ -244,7 +247,7 @@ async function createYoutubeMiddlewareToShadowDom() {
// 添加到页面上
document.body.appendChild(shadowRoot);

eval(vueScript.textContent);
// eval(vueScript.textContent);
}

async function selectChineseTranscriptAutomatically() {
Expand Down Expand Up @@ -346,11 +349,11 @@ function createTranscriptStatusElement() {
const container = document.createElement("div");
container.id = transcriptStatusElementId;
container.style.height = height;
container.style.width = "150px";
container.style.width = "200px";
container.style.backgroundColor = "white";
container.style.border = "1px solid #94a3b8";
container.style.display = "grid";
container.style.gridTemplateColumns = "1fr 1fr 1fr";
container.style.gridTemplateColumns = "1fr 1fr 1fr 1fr";
container.style.gridTemplateRows = "1fr";
container.style.alignItems = "center";
container.style.textAlign = "center";
Expand All @@ -363,27 +366,42 @@ function createTranscriptStatusElement() {
englishDiv.textContent = "English";
englishDiv.style.lineHeight = height;
englishDiv.style.height = "100%";
englishDiv.style.cursor = "not-allowed";
// englishDiv.style.backgroundColor = inActiveStatusBackgroundColor;
englishDiv.style.borderRight = "1px solid #64748b";
englishDiv.style.cursor = "not-allowed";

// 创建第二个子 div
const chineseDiv = document.createElement("div");
chineseDiv.id = transcriptStatusChineseElementId;
chineseDiv.textContent = "中文";
chineseDiv.style.lineHeight = height;
chineseDiv.style.height = "100%";
chineseDiv.style.cursor = "not-allowed";
chineseDiv.style.borderRight = "1px solid #64748b";
// chineseDiv.style.backgroundColor = inActiveStatusBackgroundColor;

// 创建第三个 div
const automationDiv = document.createElement("div");
automationDiv.textContent = "Auto";
automationDiv.style.lineHeight = height;
automationDiv.style.height = "100%";
automationDiv.style.borderRight = "1px solid #64748b";
automationDiv.style.cursor = "pointer";

// 创建第四个 div
const addVideoDiv = document.createElement("div");
addVideoDiv.textContent = "Add";
addVideoDiv.style.lineHeight = height;
addVideoDiv.style.height = "100%";
addVideoDiv.style.cursor = "pointer";
addVideoDiv.addEventListener("click", onClickStylishReaderIcon);

// 将子 div 添加到父容器
container.appendChild(englishDiv);
container.appendChild(chineseDiv);
container.appendChild(automationDiv);
container.appendChild(addVideoDiv);

automationDiv.addEventListener("click", selectChineseTranscriptAutomatically);

Expand Down
6 changes: 6 additions & 0 deletions src/webPage/youtubeTransport/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const serverUrl = import.meta.env.VITE_SERVER
function registerEventListenerFromContentScript() {
document.addEventListener('fromYoutubeVideoContentScript', async (event: any) => {
console.log('---------------------------')
console.log('添加视频')
console.log('---------------------------')
const detail = JSON.parse(event.detail)
console.log(detail.data)
const d = detail.data
Expand Down Expand Up @@ -54,6 +57,9 @@ async function checkIfVideoExist(videoId: string, token: string) {
}
onMounted(() => {
console.log('___________________________________')
console.log('youtubeTransport mounted.')
console.log('___________________________________')
registerEventListenerFromContentScript()
})
</script>
Expand Down

0 comments on commit c7548b3

Please sign in to comment.