Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
support some new sites, add hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
theowenyoung committed Nov 29, 2022
1 parent a6d7d4d commit 0bd145d
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ChangeLog

## 0.0.39

- Support linkedin.com
- Support discord discussion
- Add shortcut for switch dual show

## 0.0.37

- Support kyivindependent.com
Expand Down
10 changes: 10 additions & 0 deletions docs/hard.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@
## 1. [daily.dev](https://app.daily.dev/posts/v3SAT4LsS) 重复显示原文

微软自带翻译ok,谷歌自带翻译和插件一样的问题,似乎是原网站使用了nextjs的ssr特性之类导致的?


## 2. <https://lot51.cc/tdesc/Achievements/Descriptions/Achievement.tdesc> 无法检测动态刷新

检测到到新节点并不是父节点


## 3. br newline

1. <https://www.densediscovery.com/issues/216>
3 changes: 3 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@
"lblHotTranslatedSelectedText": {
"message": "Replace selected text with translated text instantly"
},
"lblSwitchDual":{
"message": "Show Translations/Show Dual"
},
"msgIsShowDualLanguage":{
"message": "Show dual language?"
},
Expand Down
3 changes: 3 additions & 0 deletions src/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@
"lblSwitchTranslatedAndOriginal": {
"message": "在页面的译文和原文间切换"
},
"lblSwitchDual":{
"message": "显示译文/显示双语"
},
"lblRestorePageToOriginal": {
"message": "恢复页面为原文"
},
Expand Down
3 changes: 3 additions & 0 deletions src/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"lblError": {
"message": "無法翻譯此頁面"
},
"lblSwitchDual":{
"message": "显示译文/显示双语"
},
"lblTargetLanguageHelp":{
"message": "設置翻譯後的語言,也就是你的母語"
},
Expand Down
23 changes: 23 additions & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,29 @@ if (typeof chrome.commands !== "undefined") {
action: "toggle-translation"
}, checkedLastError)
})
}else if (command === "hotkey-toggle-dual") {
if (twpConfig.get("isShowDualLanguage") === "yes") {
twpConfig.set("isShowDualLanguage", "no")
} else {
twpConfig.set("isShowDualLanguage", "yes")
}
chrome.tabs.query({
currentWindow: true,
active: true
}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "toggle-translation"
}, checkedLastError)
chrome.tabs.query({
currentWindow: true,
active: true
}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "toggle-translation"
}, checkedLastError)
})
})

} else if (command === "hotkey-swap-page-translation-service") {
chrome.tabs.query({
active: true,
Expand Down
25 changes: 24 additions & 1 deletion src/background/chrome_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,30 @@ if (typeof chrome.commands !== "undefined") {
action: "toggle-translation"
}, checkedLastError)
})
} else if (command === "hotkey-swap-page-translation-service") {
} else if (command === "hotkey-toggle-dual") {
if (twpConfig.get("isShowDualLanguage") === "yes") {
twpConfig.set("isShowDualLanguage", "no")
} else {
twpConfig.set("isShowDualLanguage", "yes")
}
chrome.tabs.query({
currentWindow: true,
active: true
}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "toggle-translation"
}, checkedLastError)
chrome.tabs.query({
currentWindow: true,
active: true
}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "toggle-translation"
}, checkedLastError)
})
})

}else if (command === "hotkey-swap-page-translation-service") {
chrome.tabs.query({
active: true,
currentWindow: true
Expand Down
9 changes: 8 additions & 1 deletion src/chrome_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"default_locale": "en",
"name": "Immersive Translate",
"description": "Let's experience immersive web translation, with bilingual simultaneous display and translation of only the important content.",
"version": "0.0.38",
"version": "0.0.38.2",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"commands": {
Expand All @@ -13,6 +13,13 @@
"mac": "MacCtrl+T"
},
"description": "__MSG_lblSwitchTranslatedAndOriginal__"
},
"hotkey-toggle-dual": {
"suggested_key": {
"default": "Ctrl+D",
"mac": "MacCtrl+D"
},
"description": "__MSG_lblSwitchDual__"
}
},

Expand Down
64 changes: 38 additions & 26 deletions src/contentScript/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,26 @@ async function getNodesThatNeedToTranslate(root,ctx,options){
}
}
for(const selector of allBlocksSelectors){
const nodes = root.querySelectorAll(selector);
for(const node of nodes){
if(currentHostname==="twitter.com" || currentHostname==="twitterdesk.twitter.com" || currentHostname==="mobile.twitter.com"){
// check language
try{
const lang = node.getAttribute("lang");
if(lang && checkIsSameLanguage(lang,[currentTargetLanguage,...neverTranslateLangs],ctx)){
continue;

if(root && root.querySelectorAll){
const nodes = root.querySelectorAll(selector);
for(const node of nodes){
if(currentHostname==="twitter.com" || currentHostname==="twitterdesk.twitter.com" || currentHostname==="mobile.twitter.com"){
// check language
try{
const lang = node.getAttribute("lang");
if(lang && checkIsSameLanguage(lang,[currentTargetLanguage,...neverTranslateLangs],ctx)){
continue;
}
}catch(e){
// ignore
// console.log("e", e)
}
}catch(e){
// ignore
// console.log("e", e)
}
}

if(isValidNode(node) && !isDuplicatedChild(allNodes,node)){
allNodes.push(node);
if(isValidNode(node) && !isDuplicatedChild(allNodes,node)){
allNodes.push(node);
}
}
}
}
Expand Down Expand Up @@ -412,6 +415,13 @@ async function getNodesThatNeedToTranslate(root,ctx,options){
originalDisplay = "block";
}

}else if(pageSpecialConfig && pageSpecialConfig.name==='discord'){
if(node.nodeName.toLowerCase() === "h3" ){
// check copy node display to block
const br = document.createElement("br");
copyNode.appendChild(br);
}

}else if(pageSpecialConfig && pageSpecialConfig.selectors){
// check is inline element
if(inlineElements.includes(node.nodeName.toLowerCase())){
Expand Down Expand Up @@ -479,18 +489,20 @@ function getContainers(root,pageSpecialConfig){
if(pageSpecialConfig.containerSelectors.length >0){
let containers =[];
for(const selector of pageSpecialConfig.containerSelectors){
const allContainer = root.querySelectorAll(pageSpecialConfig.containerSelectors);
if(allContainer){
for(const container of allContainer){
// check if brToParagraph
if(pageSpecialConfig.brToParagraph){
const pattern = new RegExp ("<br/?>[ \r\n\s]*<br/?>", "g");
container.innerHTML = container.innerHTML.replace(pattern, "</p><p>");
}


containers.push(container);
}
if(root && root.querySelectorAll){
const allContainer = root.querySelectorAll(pageSpecialConfig.containerSelectors);
if(allContainer){
for(const container of allContainer){
// check if brToParagraph
if(pageSpecialConfig.brToParagraph){
const pattern = new RegExp ("<br/?>[ \r\n\s]*<br/?>", "g");
container.innerHTML = container.innerHTML.replace(pattern, "</p><p>");
}


containers.push(container);
}
}
}
}
return containers.length>0?containers:null;
Expand Down
34 changes: 31 additions & 3 deletions src/lib/specialRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,15 @@ const specialRules = [
{
"hostname": "www.producthunt.com",
"selectors": [
"a[data-test^='post-']",
"h2",
"div.layoutCompact div[class^='styles_htmlText__']"
"div.layoutCompact div[class^='styles_htmlText__']",
".fontWeight-400.fontSize-desktop-16.color-lighter-grey",
"a[href^='/discussions/'].fontWeight-600",
"div.color-darker-grey.fontSize-14.fontWeight-400.noOfLines-undefined",
"div.color-darker-grey.fontSize-16.fontWeight-400.noOfLines-undefined"
],
"containerSelectors": [
"div[class^='styles_htmlText__']"
]
},
{
Expand All @@ -247,9 +253,11 @@ const specialRules = [
]
},
{
"name":"discord",
"hostname": "discord.com",
"selectors": [
"div[id^='message-content-']"
"div[id^='message-content-']",
"div[class^='header-']",
],
"detectLanguage":true
},
Expand Down Expand Up @@ -340,5 +348,25 @@ const specialRules = [
{
"hostname":"zlibrary24tuxziyiyfr7zd46ytefdqbqd2axkmxm4o5374ptpc52fad.onion",
"selectors":[".blogText",".jscommentsCommentText"]
},
{
"hostname":"www.sciencedirect.com",
"selectors":["h1"],
"containerSelectors":"article"
},
{
"hostname":"www.linkedin.com",
"selectors":[
".feed-shared-update-v2__description-wrapper",
],
"containerSelectors":[
"article.jobs-description__container"
]
},{
"hostname":"www.indiehackers.com",
"containerSelectors":[
".content",
],
"selectors":["h1",".feed-item__title-link"]
}
]
10 changes: 9 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"default_locale": "en",
"name": "Immersive Translate",
"description": "Let's experience immersive web translation, with bilingual simultaneous display and translation of only the important content.",
"version": "0.0.38",
"version": "0.0.38.2",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"browser_specific_settings": {
Expand All @@ -20,7 +20,15 @@
"mac": "MacCtrl+T"
},
"description": "__MSG_lblSwitchTranslatedAndOriginal__"
},
"hotkey-toggle-dual": {
"suggested_key": {
"default": "Ctrl+D",
"mac": "MacCtrl+D"
},
"description": "__MSG_lblSwitchDual__"
}

},

"icons": {
Expand Down

0 comments on commit 0bd145d

Please sign in to comment.