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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
theowenyoung committed Nov 7, 2022
1 parent da91be2 commit eb5bdcd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 50 deletions.
104 changes: 54 additions & 50 deletions src/contentScript/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,57 @@ const translateSelectors = [
"section > div.section-paragraph",
"h4","h3","h2"
],
blockSelectors:[
"div"
},
{
// TODO
hostname:["www.msdmanuals.com","localhost"],
noTranslateSelectors:[
".d-none"
]
},
{
hostname:"www.reuters.com",
containerSelector:'main',
},
{
regex:"finance\.yahoo\.com/news",
containerSelector:"[role=article]"
},
{
hostname:"www.whatsonweibo.com",
containerSelector:"#mvp-post-main"
}

]

const containerSelectorConfigs = [
{
regex:"finance\.yahoo\.com/news",
selector:"[role=article]"
function addWrapperToNode(node, wrapper){
try{

const parent = node.parentNode;
// set the wrapper as child (instead of the element)
parent.replaceChild(wrapper, node);
// set element as child of wrapper
wrapper.appendChild(node);

}catch(e){
console.error('add wrapper error',e);
}
]
}

function getAllBlocksSelectors(ctx){
function getPageSpecialConfig(ctx){
const currentUrl = ctx.tabUrl;
const currentUrlObj = new URL(currentUrl);
const currentHostname = currentUrlObj.hostname;
const currentUrlWithoutSearch = currentUrlObj.origin + currentUrlObj.pathname;
let allNodesSelectors = [];
let specialConfig = null;

for(const enhance of translateSelectors){
if(enhance.hostname){
if(!Array.isArray(enhance.hostname)){
enhance.hostname = [enhance.hostname];
}
if(enhance.hostname.indexOf(currentHostname) !== -1){
allNodesSelectors = enhance.selectors;
return enhance;
break;
}
}else if(enhance.regex){
Expand All @@ -145,37 +168,14 @@ function getAllBlocksSelectors(ctx){
for(const regex of enhance.regex){
const reg = new RegExp(regex);
if(reg.test(currentUrlWithoutSearch)){
allNodesSelectors = enhance.selectors;
isMatched = true;
break;
return enhance;
}
}
if(isMatched){
break;
}
}
}
return allNodesSelectors;
}


function getContainerSelector(ctx){
const currentUrl = ctx.tabUrl;
const currentUrlObj = new URL(currentUrl);
const currentUrlWithoutSearch = currentUrlObj.origin + currentUrlObj.pathname;
const currentHostname = currentUrlObj.hostname;
for(const containerSelector of containerSelectorConfigs){
if(containerSelector.hostname && containerSelector.hostname === currentHostname){
const container = document.querySelector(containerSelector.selector);
return container;
}else if(containerSelector.regex && new RegExp(containerSelector.regex).test(currentUrlWithoutSearch)){
const container = document.querySelector(containerSelector.selector);
if(container){
return container;
}
}
}
}

function isValidNode(node){
if(node.hasAttribute && node.hasAttribute(enhanceMarkAttributeName)){
Expand Down Expand Up @@ -265,7 +265,20 @@ function isDuplicatedChild(array,child){
}
function getNodesThatNeedToTranslate(root,ctx,options){
options = options || {};
const allBlocksSelectors = getAllBlocksSelectors(ctx);
const pageSpecialConfig = getPageSpecialConfig(ctx);
const allBlocksSelectors = pageSpecialConfig && pageSpecialConfig.selectors || []
const noTranslateSelectors = pageSpecialConfig && pageSpecialConfig.noTranslateSelectors || []
if(noTranslateSelectors.length > 0){
const noTranslateNodes = root.querySelectorAll(noTranslateSelectors.join(","));
for(const node of noTranslateNodes){
// add class notranslate
// node.classList.add("notranslate");
// add parent placeholder for position
const placeholder = document.createElement("span");
placeholder.classList.add("notranslate");
addWrapperToNode(node,placeholder);
}
}
// all block nodes, nodes should have a order from top to bottom
let allNodes = [];

Expand Down Expand Up @@ -300,13 +313,8 @@ function getNodesThatNeedToTranslate(root,ctx,options){
}
}
}else{
// const titleContainer = getTitleContainer(root,hostname);
// if(titleContainer){
// allNodes.push(titleContainer);
// }
// }
const originalRoot = root;
const contentContainer = getContainer(root,ctx);
const contentContainer = getContainer(root,pageSpecialConfig);
if(contentContainer){
root = contentContainer;

Expand Down Expand Up @@ -355,10 +363,7 @@ function getNodesThatNeedToTranslate(root,ctx,options){
const parent = node.parentNode;
const pdfContainer = document.createElement("div");
pdfContainer.style.display = "flex";
// set the wrapper as child (instead of the element)
parent.replaceChild(pdfContainer, node);
// set element as child of wrapper
pdfContainer.appendChild(node);
addWrapperToNode(node,pdfContainer);
}
}

Expand Down Expand Up @@ -394,12 +399,11 @@ function getNodesThatNeedToTranslate(root,ctx,options){

// get the main container, copy from: https://github.com/ZachSaucier/Just-Read/blob/master/content_script.js

function getContainer(root,ctx) {

const containerSelector = getContainerSelector(ctx);
function getContainer(root,pageSpecialConfig){


if(containerSelector){
const container = root.querySelector(containerSelector);
if(pageSpecialConfig && pageSpecialConfig.containerSelector){
const container = root.querySelector(pageSpecialConfig.containerSelector);
if(container){
return container;
}
Expand Down
1 change: 1 addition & 0 deletions src/contentScript/pageTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ Promise.all([twpConfig.onReady(), getTabUrl()])
const piecesToTranslateNow = []
piecesToTranslate.forEach(ptt => {
if (!ptt.isTranslated) {

if (bottomIsInScreen(ptt.topElement) || topIsInScreen(ptt.bottomElement)) {
ptt.isTranslated = true
piecesToTranslateNow.push(ptt)
Expand Down

0 comments on commit eb5bdcd

Please sign in to comment.