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

Commit

Permalink
support pdf translate split left and right
Browse files Browse the repository at this point in the history
  • Loading branch information
theowenyoung committed Nov 7, 2022
1 parent 2ec9d81 commit 0c27226
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
45 changes: 34 additions & 11 deletions src/contentScript/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const enhanceHtmlTagsNoTranslate = ['TITLE', 'SCRIPT', 'STYLE', 'TEXTAREA', 'SVG
const blockElements = [
'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'OL', 'P', 'TABLE', 'UL'
];

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

const inlineElements = [
"a",
"abbr",
Expand Down Expand Up @@ -89,14 +98,8 @@ const translateSelectors = [

]
},
{
regex:[
"translatewebpages.org/result/.+$",
],
selectors:[
'div'
]
}
pdfSelectorsConfig

]

const containerSelectorConfigs = [
Expand All @@ -110,7 +113,7 @@ function getAllBlocksSelectors(){
const currentUrl = window.location.href;
const currentUrlObj = new URL(currentUrl);
const currentHostname = currentUrlObj.hostname;

const currentUrlWithoutSearch = currentUrlObj.origin + currentUrlObj.pathname;
let allNodesSelectors = [];

for(const enhance of translateSelectors){
Expand All @@ -129,7 +132,7 @@ function getAllBlocksSelectors(){
let isMatched = false;
for(const regex of enhance.regex){
const reg = new RegExp(regex);
if(reg.test(currentUrl)){
if(reg.test(currentUrlWithoutSearch)){
allNodesSelectors = enhance.selectors;
isMatched = true;
break;
Expand All @@ -147,12 +150,13 @@ const allBlocksSelectors = getAllBlocksSelectors();
function getContainerSelector(){
const currentUrl = window.location.href;
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(window.location.href)){
}else if(containerSelector.regex && new RegExp(containerSelector.regex).test(currentUrlWithoutSearch)){
const container = document.querySelector(containerSelector.selector);
if(container){
return container;
Expand Down Expand Up @@ -235,6 +239,10 @@ function getNodesThatNeedToTranslate(root,hostname,options){
// all block nodes, nodes should have a order from top to bottom
let allNodes = [];

const currentUrl = window.location.href;
const currentUrlObj = new URL(currentUrl);
const currentUrlWithoutSearch = currentUrlObj.origin + currentUrlObj.pathname;
const currentHostname = currentUrlObj.hostname;
let currentTargetLanguage = twpConfig.get("targetLanguage")

// check sites
Expand Down Expand Up @@ -293,6 +301,21 @@ function getNodesThatNeedToTranslate(root,hostname,options){
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
})

// is pdf, if pdf, then treat it as a special case
const isPdf = new RegExp(pdfSelectorsConfig.regex).test(currentUrlWithoutSearch);
if(isPdf){
// add flex container to div
for(const node of allNodes){
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);
}
}

for(const node of allNodes){
// check if there is a copy already
const previousSibling = node.previousSibling;
Expand Down
11 changes: 10 additions & 1 deletion src/contentScript/pageTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,17 @@ Promise.all([twpConfig.onReady(), getTabHostName()])
}

function encapsulateTextNode(node) {
const currentUrl = window.location.href;
const currentUrlObj = new URL(currentUrl);
const currentUrlWithoutSearch = currentUrlObj.origin + currentUrlObj.pathname;
const currentHostname = currentUrlObj.hostname;
const isPdf = new RegExp(pdfSelectorsConfig.regex).test(currentUrlWithoutSearch);
const fontNode = document.createElement("font")
fontNode.setAttribute("style", "vertical-align: inherit;border-bottom: 2px solid #72ECE9;")
let style = 'vertical-align: inherit;'
if (!isPdf) {
style+='border-bottom: 2px solid #72ECE9;'
}
fontNode.setAttribute("style", style)
fontNode.textContent = node.textContent

node.replaceWith(fontNode)
Expand Down

0 comments on commit 0c27226

Please sign in to comment.