Skip to content

Commit

Permalink
feat(hjenglishko): add hjenglishKo dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
tonytonyjan committed Jul 13, 2020
1 parent 818a035 commit 0925b90
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ browser.webRequest.onBeforeSendHeaders.addListener(
return { requestHeaders };
},
{
urls: ["https://dict.hjenglish.com/jp/jc/*"],
urls: ["https://dict.hjenglish.com/*"],
types: ["xmlhttprequest"],
},
(() => {
Expand Down
89 changes: 4 additions & 85 deletions src/dictionaries/hjenglish.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,9 @@
import React from "react";
import hjenglishCore from "./hjenglishCore";

const hjenglish = async (query) => {
const response = await fetch(
`https://dict.hjenglish.com/jp/jc/${encodeURIComponent(query)}`
const hjenglish = async (query) =>
hjenglishCore(
fetch(`https://dict.hjenglish.com/jp/jc/${encodeURIComponent(query)}`)
);
if (!response.ok) throw new Error("not ok");
const dom = new DOMParser().parseFromString(
await response.text(),
"text/html"
);
const panes = Array.from(dom.querySelectorAll(".word-details-pane")).map(
(i) => ({
word: i.querySelector(".word-text")?.textContent.trim(),
pronunciation: i
.querySelector(".pronounces")
.textContent.trim()
.split(/\s+/g)
.slice(0, 2)
.join(" "),
simpleHtml: (() => {
const result = i.querySelector(".simple");
result.querySelectorAll("h2").forEach((node) => {
const div = document.createElement("div");
div.classList.add("lead");
div.append(...node.childNodes);
node.replaceWith(div);
});
result.querySelectorAll("ul").forEach((node) => {
const ol = document.createElement("ol");
node
.querySelectorAll("li > span")
.forEach((node) => node.parentNode.removeChild(node));
ol.append(...node.childNodes);
node.replaceWith(ol);
});
return result.innerHTML;
})(),
groups: Array.from(i.querySelectorAll(".detail-groups > dl")).map(
(i) => ({
partOfSpeech: i.querySelector("dt").textContent.trim(),
definitions: Array.from(i.querySelectorAll("dd")).map((i) => ({
summary: i.querySelector("h3")?.textContent?.trim(),
examples: Array.from(i.querySelectorAll("ul > li")).map((i) => ({
from: i.querySelector(".def-sentence-from")?.textContent?.trim(),
to: i.querySelector(".def-sentence-to")?.textContent?.trim(),
})),
})),
})
),
})
);
if (panes.length === 0) return null;
return (
<div>
{panes.map((i, index) => (
<div key={index}>
<div className="lead">
{i.word} <span className="text-secondary">{i.pronunciation}</span>
{/* <div dangerouslySetInnerHTML={{ __html: i.simpleHtml }} /> */}
{i.groups.map((i, index) => (
<div key={index}>
<div className="lead">{i.partOfSpeech}</div>
<ol>
{i.definitions.map((i, index) => (
<li key={index}>
{i.summary}{" "}
<ul>
{i.examples.map((i, index) => (
<li key={index}>
{i.from}
<br />
<span className="text-secondary">{i.to}</span>
</li>
))}
</ul>
</li>
))}
</ol>
</div>
))}
</div>
</div>
))}
</div>
);
};

hjenglish.displayName = "滬江小D";
hjenglish.fullName = "滬江小D 日中字典";
Expand Down
85 changes: 85 additions & 0 deletions src/dictionaries/hjenglishCore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React from "react";

export default async (fetchPromise) => {
const response = await fetchPromise;
if (!response.ok) throw new Error("not ok");
const dom = new DOMParser().parseFromString(
await response.text(),
"text/html"
);
const panes = Array.from(dom.querySelectorAll(".word-details-pane")).map(
(i) => ({
word: i.querySelector(".word-text")?.textContent.trim(),
pronunciation: i
.querySelector(".pronounces")
.textContent.trim()
.split(/\s+/g)
.slice(0, 2)
.join(" "),
simpleHtml: (() => {
const result = i.querySelector(".simple");
result.querySelectorAll("h2").forEach((node) => {
const div = document.createElement("div");
div.classList.add("lead");
div.append(...node.childNodes);
node.replaceWith(div);
});
result.querySelectorAll("ul").forEach((node) => {
const ol = document.createElement("ol");
node
.querySelectorAll("li > span")
.forEach((node) => node.parentNode.removeChild(node));
ol.append(...node.childNodes);
node.replaceWith(ol);
});
return result.innerHTML;
})(),
groups: Array.from(i.querySelectorAll(".detail-groups > dl")).map(
(i) => ({
partOfSpeech: i.querySelector("dt").textContent.trim(),
definitions: Array.from(i.querySelectorAll("dd")).map((i) => ({
summary: i.querySelector("h3")?.textContent?.trim(),
examples: Array.from(i.querySelectorAll("ul > li")).map((i) => ({
from: i.querySelector(".def-sentence-from")?.textContent?.trim(),
to: i.querySelector(".def-sentence-to")?.textContent?.trim(),
})),
})),
})
),
})
);
if (panes.length === 0) return null;
return (
<div>
{panes.map((i, index) => (
<div key={index}>
<div className="lead">
{i.word} <span className="text-secondary">{i.pronunciation}</span>
{/* <div dangerouslySetInnerHTML={{ __html: i.simpleHtml }} /> */}
{i.groups.map((i, index) => (
<div key={index}>
<div className="lead">{i.partOfSpeech}</div>
<ol>
{i.definitions.map((i, index) => (
<li key={index}>
{i.summary}{" "}
<ul>
{i.examples.map((i, index) => (
<li key={index}>
{i.from}
<br />
<span className="text-secondary">{i.to}</span>
</li>
))}
</ul>
</li>
))}
</ol>
</div>
))}
</div>
</div>
))}
</div>
);
};
11 changes: 11 additions & 0 deletions src/dictionaries/hjenglishKo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hjenglishCore from "./hjenglishCore";

const hjenglishKo = async (query) =>
hjenglishCore(
fetch(`https://dict.hjenglish.com/kr/${encodeURIComponent(query)}`)
);

hjenglishKo.displayName = "滬江小D韓文";
hjenglishKo.fullName = "滬江小D 韓中字典";

export default hjenglishKo;
2 changes: 2 additions & 0 deletions src/dictionaries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cambridge from "dictionaries/cambridge";
import cambridgeEnZh from "dictionaries/cambridgeEnZh";
import thesaurus from "dictionaries/thesaurus";
import hjenglish from "dictionaries/hjenglish";
import hjenglishKo from "dictionaries/hjenglishKo";

export default {
yahoo,
Expand All @@ -20,4 +21,5 @@ export default {
thesaurus,
cambridgeEnZh,
hjenglish,
hjenglishKo,
};
4 changes: 4 additions & 0 deletions stories/components/Dictionary.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import cambridge from "dictionaries/cambridge";
import cambridgeEnZh from "dictionaries/cambridgeEnZh";
import thesaurus from "dictionaries/thesaurus";
import hjenglish from "dictionaries/hjenglish";
import hjenglishKo from "dictionaries/hjenglishKo";

const DictContainer = ({ query, dict }) => {
const [content, setContent] = useState(null);
Expand Down Expand Up @@ -78,6 +79,9 @@ export const Thesaurus = () => (
export const Hjenglish = () => (
<DictContainer query={text("Query", "試験")} dict={hjenglish} />
);
export const HjenglishKo = () => (
<DictContainer query={text("Query", "포옹")} dict={hjenglishKo} />
);

export default {
title: "Dictionary",
Expand Down

0 comments on commit 0925b90

Please sign in to comment.