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

Commit

Permalink
support custom page special rules, fix gmail, support discord, telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
theowenyoung committed Nov 13, 2022
1 parent 4a3bdce commit 44eba65
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 166 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log


## 0.0.24

- Support discord.com
- Support web.telegram.org/z/
- Fix mail.google.com , now it'll be better for long articles.
- Support Custom Page Special Rules

## 0.0.23

- Support mail.google.com
Expand Down
6 changes: 6 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,5 +483,11 @@
},
"msgIsShowDualLanguage":{
"message": "Is Show dual language?"
},
"optionsSpecialRules":{
"message": "Special Page Rules"
},
"optionsSpecialRulesNotes":{
"message":"This is an advanced option, please make sure you know what you are doing before editing. The priority of the rules added here is higher than that of the <a href=\"https://github.com/immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js\">rules built into the extension</a><br>It's actually not very complicated, it's essentially some css selectors, and here's a <a href=\"https://github.com/immersive-translate/immersive-translate/wiki/Special-Rules-Tutorial\">tutorial </a> to help you.<br>The ultimate goal is to make the default rules for this extension better, so please consider contributing your rules to the <a href=\"https://github.com/immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js\">project</a>"
}
}
9 changes: 9 additions & 0 deletions src/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,15 @@
},
"msgOpenOptions":{
"message": "选项设置"
},
"optionsSpecialRules":{
"message": "特殊页面翻译规则"
},
"optionsSpecialRulesNotes":{
"message":"这是一个高级选项,请确保你在编辑之前知道自己在做什么! 这里添加的规则的优先级高于内置的<a href=\"https://github.com/immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js\">规则</a><br>别被吓到,其实也不是很复杂啦,本质上就是一些css选择器,这里有一个<a href=\"https://github.com/immersive-translate/immersive-translate/wiki/Special-Rules -Tutorial\">教程</a>来帮助你。<br>我们最终目标是让这个扩展的默认规则更人性化,所以请考虑将你的规则贡献给<a href=\"https://github.com /immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js\">本项目</a>。"
},
"lblTelegram":{
"message": "加Telegram群"
}

}
10 changes: 10 additions & 0 deletions src/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,15 @@
},
"msgOpenOptions":{
"message": "選項設置"
},
"optionsSpecialRules":{
"message": "特殊頁面翻譯規則"
},
"optionsSpecialRulesNotes":{
"message":"這是一個高級選項,請確保你在編輯之前知道自己在做什麼! 這裡添加的規則的優先級高於內置的<a href=\"https://github.com/immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js\">規則</a><br>別被嚇到,其實也不是很複雜啦,本質上就是一些css選擇器,這裡有一個<a href=\"https://github.com/immersive-translate/immersive-translate/wiki/Special-Rules -Tutorial\">教程</a>來幫助你。<br>我們最終目標是讓這個擴展的默認規則更人性化,所以請考慮將你的規則貢獻給<a href=\"https://github.com /immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js\">本項目</a>。"
},
"lblTelegram":{
"message": "加Telegram群"
}

}
4 changes: 2 additions & 2 deletions 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.23",
"version": "0.0.24",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"commands": {
Expand Down Expand Up @@ -101,7 +101,7 @@
"run_at": "document_end",
"all_frames": true,
"match_about_blank": true,
"js": ["/lib/i18n.js", "/contentScript/showOriginal.js", "/contentScript/enhance.js", "/contentScript/pageTranslator.js","/contentScript/play.js", "/contentScript/translateSelected.js", "/contentScript/showTranslated.js"]
"js": ["/lib/i18n.js", "/contentScript/showOriginal.js", "/lib/specialRules.js", "/contentScript/enhance.js", "/contentScript/pageTranslator.js","/contentScript/play.js", "/contentScript/translateSelected.js", "/contentScript/showTranslated.js"]
},
{
"matches": ["<all_urls>"],
Expand Down
173 changes: 11 additions & 162 deletions src/contentScript/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const enhanceMarkAttributeName = "data-translationmark";
const enhanceOriginalDisplayValueAttributeName = "data-translationoriginaldisplay";
const enhanceHtmlTagsInlineIgnore = ['BR', 'CODE', 'KBD', 'WBR'] // and input if type is submit or button, and pre depending on settings
const enhanceHtmlTagsNoTranslate = ['TITLE', 'SCRIPT', 'STYLE', 'TEXTAREA', 'SVG', 'svg'] //TODO verificar porque 'svg' é com letras minúsculas
const blockElements = [
let blockElements = [
'H1', 'H2', 'H3', 'H4', 'H5', 'H6','TABLE', 'OL', 'P','LI'
];
if (twpConfig.get('translateTag_pre') !== 'yes') {
Expand All @@ -14,12 +14,8 @@ const headingElements = ['h1' ];

const pdfSelectorsConfig = {
regex:
"translatewebpages.org/result/.+$",
selectors:[
'div'
],
style:"none",
};
"translatewebpages.org/result/.+$"
};

const inlineElements = [
"a",
Expand Down Expand Up @@ -57,160 +53,6 @@ const inlineElements = [
"var",
];

const translateSelectors = [
{
hostname:["twitter.com","tweetdeck.twitter.com","mobile.twitter.com"],
selectors:[
'[data-testid="tweetText"]',".tweet-text" ,".js-quoted-tweet-text"
]
},
{
hostname:"news.ycombinator.com",
selectors:[
".titleline >a",
'.comment',
'.toptext'

]
},
{
hostname:"www.reddit.com",
selectors:[
// "[data-adclicklocation=title]",
"h1",
"[data-click-id=body] h3","[data-click-id=background] h3"
],
containerSelectors:[
"[data-testid=comment]",
"[data-adclicklocation=media]"
]
},
{
hostname:"old.reddit.com",
selectors:[
"a.title",
],
containerSelectors:[
"[role=main] .md-container"
]
},
{
regex:"finance\.yahoo\.com\/$",
selectors:[
"h3"
]

},
{
regex:["www\.bloomberg\.com\/[A-Za-z0-9]+$","www\.bloomberg\.com\/$"],
selectors:[
"article h3",
"article .single-story-module__headline-link",
"article [data-tracking-type=Story]",
"article .story-list-story__info__headline"

]
},
pdfSelectorsConfig,
{
hostname:"www.cell.com",
selectors:[
"div.section-paragraph > div.section-paragraph > div.section-paragraph",
"section > div.section-paragraph",
"h4","h3","h2"
],
},
{
// TODO
hostname:["www.msdmanuals.com","localhost"],
noTranslateSelectors:[
".d-none"
]
},
{
hostname:"www.reuters.com",
containerSelectors:'main',
},
{
regex:"finance\.yahoo\.com/news",
containerSelectors:"[role=article]"
},
{
hostname:"www.whatsonweibo.com",
containerSelectors:"#mvp-post-main"
},
{
hostname:["www.wsj.com","www.economist.com"],
containerSelectors:"main"
},

{
hostname:["mail.jabber.org","antirez.com"],
selectors:["pre"],
containerSelectors: "pre",
style: 'none'
},
{
hostname:"github.com",
containerSelectors:".markdown-body"
},
{
hostname:"www.youtube.com",
selectors:["#content-text"]
},
{
hostname:"www.facebook.com",
selectors:["div[data-ad-comet-preview=message] > div > div","div[role=article] > div > div > div > div > div > div > div > div "]

},
{
regex:'\.substack\.com\/',
selectors:[
".post-preview-title",
".post-preview-description"
],
containerSelectors:[
".post"
]
},
{
hostname:"www.nature.com",
containerSelectors:"article"
},{
hostname:"seekingalpha.com",
containerSelectors:"div.wsb_section",
brToParagraph: true
},{
hostname:"hn.algolia.com",
selectors:[".Story_title"]
},{
hostname:"read.readwise.io",
selectors:['div[class^="_titleRow_"]','div[class^="_description_"]'],
containerSelectors:[
"#document-text-content"
]
},{
hostname:"www.inoreader.com",
selectors:[".article_title",],
containerSelectors:[
".article_content"
]
},
{
hostname:"mail.google.com",
selectors:["h2[data-thread-perm-id]","span[data-thread-id]"],
containerSelectors:[
"div[data-message-id] > div > div[class='']"
]
},{
hostname:"www.producthunt.com",
selectors:["a[data-test^='post-']",'h2',"div.layoutCompact div[class^='styles_htmlText__']"],
},{
hostname:"arxiv.org",
selectors:["blockquote.abstract","h1"]
}

]

function addWrapperToNode(node, wrapper){
try{
Expand All @@ -231,9 +73,12 @@ function getPageSpecialConfig(ctx){
const currentUrlObj = new URL(currentUrl);
const currentHostname = currentUrlObj.hostname;
const currentUrlWithoutSearch = currentUrlObj.origin + currentUrlObj.pathname;

// merge spcialRules

let specialConfig = null;

for(const enhance of translateSelectors){
for(const enhance of specialRules){
if(enhance.hostname){
if(!Array.isArray(enhance.hostname)){
enhance.hostname = [enhance.hostname];
Expand Down Expand Up @@ -365,6 +210,10 @@ async function getNodesThatNeedToTranslate(root,ctx,options){
const currentHostname = currentUrlObj.hostname;
let currentTargetLanguage = twpConfig.get("targetLanguage")

// special for mail.google.com, cause there are too many table, we should remove table
if(pageSpecialConfig && pageSpecialConfig.blockElements){
blockElements = pageSpecialConfig.blockElements;
}
// check sites
if(allBlocksSelectors.length>0){
for(const selector of allBlocksSelectors){
Expand Down
12 changes: 12 additions & 0 deletions src/contentScript/pageTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ Promise.all([twpConfig.onReady(), getTabUrl()])
const htmlTagsInlineText = ['#text', 'A', 'ABBR', 'ACRONYM', 'B', 'BDO', 'BIG', 'CITE', 'DFN', 'EM', 'I', 'LABEL', 'Q', 'S', 'SMALL', 'SPAN', 'STRONG', 'SUB', 'SUP', 'U', 'TT', 'VAR']
const htmlTagsInlineIgnore = ['BR', 'CODE', 'KBD', 'WBR'] // and input if type is submit or button, and pre depending on settings
const htmlTagsNoTranslate = ['TITLE', 'SCRIPT', 'STYLE', 'TEXTAREA', 'SVG', 'svg'] //TODO verificar porque 'svg' é com letras minúsculas
const specialRulesConfigs = twpConfig.get('specialRules');
if(Array.isArray(specialRulesConfigs) && specialRulesConfigs.length > 0){
for(const specialRuleString of specialRulesConfigs){
// add to specialRules
try{
const specialRule = JSON.parse(specialRuleString);
specialRules.unshift(specialRule);
}catch(e){
console.warn(`Error parsing special rule: ${specialRuleString}`)
}
}
}

if (twpConfig.get('translateTag_pre') !== 'yes') {
htmlTagsInlineIgnore.push('PRE')
Expand Down
7 changes: 7 additions & 0 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const twpConfig = (function () {
targetLanguages: [], // "en", "es", "de"
alwaysTranslateSites: [],
neverTranslateSites: [],
specialRules: [],
sitesToTranslateWhenHovering: [],
langsToTranslateWhenHovering: [],
alwaysTranslateLangs: [],
Expand Down Expand Up @@ -400,12 +401,18 @@ const twpConfig = (function () {
removeFromArray("alwaysTranslateSites", hostname);
removeFromArray("sitesToTranslateWhenHovering", hostname);
};
twpConfig.addRuleToSpecialRules = function (hostname) {
addInArray("specialRules", hostname);
};
twpConfig.addKeyWordTocustomDictionary = function (key, value) {
addInMap("customDictionary", key, value);
};
twpConfig.removeSiteFromNeverTranslate = function (hostname) {
removeFromArray("neverTranslateSites", hostname);
};
twpConfig.removeRuleFromSpecialRules = function (hostname) {
removeFromArray("specialRules", hostname);
};
twpConfig.removeKeyWordFromcustomDictionary = function (keyWord) {
removeFromMap("customDictionary", keyWord);
};
Expand Down
10 changes: 10 additions & 0 deletions src/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ void (function () {
element.textContent = text;
}

for (const element of root.querySelectorAll("[data-i18n-html]")) {
let text = getMessage(
element.getAttribute("data-i18n-html"),
undefined
);
if (!text) {
continue;
}
element.innerHTML = text;
}
translateAttributes(root, "title");
translateAttributes(root, "placeholder");
translateAttributes(root, "label");
Expand Down
4 changes: 2 additions & 2 deletions 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.23",
"version": "0.0.24",
"homepage_url": "https://github.com/immersive-translate/immersive-translate",

"browser_specific_settings": {
Expand Down Expand Up @@ -126,7 +126,7 @@
"run_at": "document_end",
"all_frames": true,
"match_about_blank": true,
"js": ["/lib/i18n.js", "/contentScript/showOriginal.js", "/contentScript/enhance.js", "/contentScript/pageTranslator.js", "/contentScript/play.js","/contentScript/translateSelected.js", "/contentScript/showTranslated.js"]
"js": ["/lib/i18n.js", "/contentScript/showOriginal.js","/lib/specialRules.js", "/contentScript/enhance.js", "/contentScript/pageTranslator.js", "/contentScript/play.js","/contentScript/translateSelected.js", "/contentScript/showTranslated.js"]
},
{
"matches": ["<all_urls>"],
Expand Down
13 changes: 13 additions & 0 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ <h3 class="w3-wide"><b>Immersive Translate</b></h3>
<a href="https://github.com/immersive-translate/immersive-translate/blob/main/CHANGELOG.md" class="w3-bar-item w3-button w3-padding" data-i18n="lblReleaseNotes">Release Notes</a>
<a href="https://github.com/immersive-translate/immersive-translate/issues" target="blank"
class="w3-bar-item w3-button w3-padding" data-i18n="lblReport">Report a problem</a>
<a href="https://t.me/+rq848Z09nehlOTgx" target="blank"
class="w3-bar-item w3-button w3-padding" data-i18n="lblTelegram">Telegram Group</a>
<!-- <a href="https://crowdin.com/project/translate-web-pages" target="blank" class="w3-bar-item w3-button w3-padding" data-i18n="lblTranslateInterface">Translate the interface</a> -->
</nav>

Expand Down Expand Up @@ -176,6 +178,17 @@ <h3 class="w3-wide"><b>Immersive Translate</b></h3>
</div>
<ul id="customDictionary" class="w3-ul w3-card-4 w3-margin w3-round list"></ul>
</div>
<br>
<div class="w3-display-container w3-padding w3-margin-top w3-margin-bottom">
<div class="w3-display-left">
<p><b data-i18n="optionsSpecialRules">Special Rules (Advanced)</b></p>
</div>
</div>
<p data-i18n-html="optionsSpecialRulesNotes" style="color:#999;">This is an advanced option, please make sure you know what you are doing before editing. The priority of the rules added here is higher than that of the <a href="https://github.com/immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js">rules built into the extension</a><br>It's actually not very complicated, it's essentially some css selectors, and here's a <a href="https://github.com/immersive-translate/immersive-translate/wiki/Special-Rules-Tutorial">tutorial </a> to help you.<br>The ultimate goal is to make the default rules for this extension better, so please consider contributing your rules to the <a href="https://github.com/immersive-translate/immersive-translate/blob/main/src/lib/specialRules.js">project</a></p>
<textarea rows="5" class="w3-margin-bottom" id="specialRule" placeholder='{"hostname":"twitter.com","selectors":["[data-testid=tweetText]"]}' style="width:100%"></textarea>
<button id="addToSpecialRules" class="w3-button w3-blue w3-round w3-margin-right add"
data-i18n="lblAdd">Add</button>
<ul id="specialRules" class="w3-ul w3-card-4 w3-margin w3-round list"></ul>
</div>

<div id="translations" class="w3-container">
Expand Down
Loading

0 comments on commit 44eba65

Please sign in to comment.