Skip to content

Commit

Permalink
状态更改
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlb0224 committed Jun 7, 2024
1 parent eee3dfd commit db883cd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 36 deletions.
20 changes: 14 additions & 6 deletions src/modules/toolLink.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from "../../package.json";
import { refSearch, createItemByZotero, searchItem } from "../utils/cite";
import { refSearch, createItemByZotero, searchItem, ruleSearch, ruleTestSingle } from "../utils/cite";
import { getPrefT, setPref } from "../utils/prefs";
import { isDebug, openAnnotation } from "../utils/zzlb";
function register() {
Expand All @@ -23,7 +23,7 @@ function readerToolbarCallback(
let enable = getPrefT("show-query-href", false);
const root =
doc.querySelector("body") || (doc.querySelector("div") as HTMLElement);

const toolbarBtn = ztoolkit.UI.createElement(doc, "div", {
namespace: "html",
id: `${config.addonRef}-space-button`,
Expand All @@ -46,6 +46,9 @@ function readerToolbarCallback(
enable = false;
toolbarBtn.style.background = "";
p?.removeEventListener("DOMSubtreeModified", DOMSubtreeModified);



} else {
enable = true;
toolbarBtn.style.background = "#ddd";
Expand All @@ -54,7 +57,7 @@ function readerToolbarCallback(
DOMSubtreeModified,
false,
);
}

},
},
],
Expand All @@ -68,14 +71,18 @@ function readerToolbarCallback(
}
async function DOMSubtreeModified(e: Event) {
const p = e.target as HTMLDivElement;
ztoolkit.log("DOMSubtreeModified",p.classList)
if (p.classList.contains("primary")) {
const refRows = p.querySelectorAll(".reference-row");
ztoolkit.log("DOMSubtreeModified",p.classList,refRows.length)
for (const refRow of refRows as NodeListOf<HTMLDivElement>) {
const m = await refSearch(refRow.textContent || "");
const text=refRow.textContent || ""

const m = ruleSearch(text);
{
ztoolkit.log(
refRow.dataset,
refRow.textContent,
text,
"在这里判断是否在我的文库当中,不在文库当中显示添加到文库按钮",
m,
);
Expand Down Expand Up @@ -260,6 +267,7 @@ async function DOMSubtreeModified(e: Event) {
refRow,
);
} else {
ruleTestSingle(text)
ztoolkit.UI.appendElement(
{
tag: "span",
Expand All @@ -274,7 +282,7 @@ async function DOMSubtreeModified(e: Event) {
refRow,
);
}
break;
// break;
}
}
}
Expand Down
95 changes: 65 additions & 30 deletions src/utils/cite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const author3 = space + `(?<author>[^\\(]+?)` + space;
const title = space + `(?<title>.+?)` + space;
const title1 = space + `(?<title>[^\\.]+?)` + space;
const journal = `\\s*(?<journal>.+?)\\s*`;
const journal1 = `\\s*(?<journal>[^?]+?)\\s*`;
const year = `\\s*(?<year>[\\d]+)[a-z]?\\s*`;
const year1 = `${space}\\(${space}(?<year>[\\d]+)[a-z]?${space}\\)${space}`;
const page = space + `(?<page>[\\d–-]+)` + space;
const issue = space + `(?<issue>[\\d]*)` + space;
const volume = space + `(?<volume>[\\d]*)` + space;
const volume1 = space + `\\(${space}(?<volume>[\\d]*)${space}\\)` + space;
const doi = `${space}(?:doi:|DOI:|https://doi.org/)${space}(?<doi>.*)[\\.]?${space}`;
const doi1 = `${space}(?:doi:|DOI:|https://doi.org/)${space}(?<doi>.*)?[\\.]?${space}`;
const doi = `${space}(?:doi:|DOI:|https://doi.org/|https:// doi.org/)${space}(?<doi>.*)[\\.]?${space}`;
const doi1 = `${space}(?:doi:|DOI:|https://doi.org/|https:// doi.org/)${space}(?<doi>.*)?[\\.]?${space}`;
const nn = new RegExp(
`${author2},${year}\\.${title1}\\.${journal}${issue},${page}\\.`,
);
Expand Down Expand Up @@ -53,14 +54,50 @@ const a8 = new RegExp(
const regexps = [apa_doi0, apa1, apa2, apa3, apa4, a5, a6, a7, a8];
const rules = [
{
title: "apa Article 在page前面",
re: apa_doi0,
},
{
re: apa1,
},
{
re: apa2,
},
{
re: apa3,
},
{
re: apa4,
},
{
re: a5,
},
{
re: a6,
},
{
re: a7,
},
{
re: a8,
},
{
title: "apa page前面带Article,标题还带了个问号结尾",
re: new RegExp(
`${author3}\\(${year}\\)${title1}\\.${journal}${issue},${space}Article${page}\\.`,
`${author3}\\(${year}\\)${space}\\.${title}[\\.?]${journal1},${issue},${space}Article${page}\\.`,
),
examples: [
"Moore, K., Buchmann, A., Månsson, M., & Fisher, D. (2021). Authenticity in tourism theory and experience. Practically indispensable and theoretically mischievous? Annals of Tourism Research, 89, Article 103208.",
],
},
{
title: "apa DOI",
re: new RegExp(
`${author3}\\(${year}\\)${space}\\.${title}[\\.?]${journal1},${issue},${page}\\.${doi}`,
),
examples: [
"Balakrishnan, J., & Dwivedi, Y. K. (2021). Conversational commerce: Entering the next stage of AI-powered digital assistants. Annals of Operations Research, 1–35. https:// doi.org/10.1007/s10479-021-04049-5",
],
},
];
export function ruleSearch(str: string) {
for (let index = 0; index < rules.length; index++) {
Expand All @@ -78,15 +115,20 @@ export function ruleSearch(str: string) {

export function ruleTestInner(index: number | undefined = undefined) {
for (let i = 0; i < rules.length; i++) {
if (index !== undefined && index !== i) continue;
if (index !== undefined && index !== i && index + rules.length !== i)
continue;
const rule = rules[i];
for (const str of rule.examples) {
const m = str.match(rule.re);
if (m) {
ztoolkit.log("OK", i, str);
} else {
ztoolkit.log("error", i, str, rule);
if (rule.examples) {
for (const str of rule.examples) {
const m = str.match(rule.re);
if (m) {
ztoolkit.log("OK", i, str, m.groups);
} else {
ztoolkit.log("error", i, str, rule);
}
}
} else {
ztoolkit.log("OK", i, "没有测试用例");
}
}
}
Expand All @@ -96,30 +138,23 @@ export function ruleTestCross() {

for (let ei = 0; ei < rules.length; ei++) {
const e = rules[ei];
for (const str of e.examples) {
const m = str.match(rule.re);
if (m) {
if (ei == index) {
ztoolkit.log("OK", index, str);
if (e.examples) {
for (const str of e.examples) {
const m = str.match(rule.re);
if (m) {
if (ei == index) {
ztoolkit.log("OK", index, str);
} else {
ztoolkit.log("error", index, ei, str, rule);
}
} else {
ztoolkit.log("error", index, ei, str, rule);
}
} else {
if (ei == index) {
ztoolkit.log("error", index, ei, str, rule);
if (ei == index) {
ztoolkit.log("error", index, ei, str, rule);
}
}
}
}
}

const m = str.match(rule.re);
if (m) {
// ztoolkit.log(rStr, m);
const groups = m.groups;
if (groups) {
return { index, rule, groups };
}
}
}
}
export function ruleTestSingle(str: string) {
Expand Down

0 comments on commit db883cd

Please sign in to comment.