Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
organic-nailer committed Dec 1, 2021
0 parents commit 891bda4
Show file tree
Hide file tree
Showing 7 changed files with 545 additions and 0 deletions.
21 changes: 21 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
chrome.runtime.onInstalled.addListener(() => {
// Page actions are disabled by default and enabled on select tabs
chrome.action.disable();

// Clear all rules to ensure only our expected rules are set
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// Declare a rule to enable the action on example.com pages
let exampleRule = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: "www.jstage.jst.go.jp" },
}),
],
actions: [new chrome.declarativeContent.ShowAction()],
};

// Finally, apply our new array of rules
let rules = [exampleRule];
chrome.declarativeContent.onPageChanged.addRules(rules);
});
});
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "jstage論文参考文献自動生成くん",
"manifest_version": 3,
"version": "0.1",
"author": "fastriver_org",
"icons": {
"48": "icon.png"
},
"action": {
"default_title": "論文参考文献自動生成くん",
"default_popup": "popup.html",
"default_icon": {
"48": "icon.png"
}
},
"background": {
"service_worker": "background.js"
},
"permissions": [
"activeTab",
"storage",
"declarativeContent"
]
}
21 changes: 21 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<meta charset="utf-8" />
<style>
body {
width: 400px;
}
textarea {
width: 100%;
}
</style>
<script src="popup.js" defer></script>
</head>
<body>
<p id="generated_path"></p>
<button id="apply">コピーする</button>
<p>Template:</p>
<textarea id="input_template" rows="2"></textarea>
<button id="change_template">テンプレート更新</button>
</body>
</html>
95 changes: 95 additions & 0 deletions popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const outView = document.getElementById("generated_path");
const applyButton = document.getElementById("apply");

const inputTemplate = document.getElementById("input_template");
const templateButton = document.getElementById("change_template");

const DEFAULT_TEMPLATE = `$authors「$title」($journal, $year 年, $volume 巻 $issue 号, $page)`;

const TEMPLATE_KEY = "TEMPLATE_KEY";

chrome.storage.local.get([TEMPLATE_KEY], (value) => {
if (value.hasOwnProperty(TEMPLATE_KEY)) {
inputTemplate.value = value[TEMPLATE_KEY];
} else {
inputTemplate.value = DEFAULT_TEMPLATE;
}
});

templateButton.addEventListener("click", () => {
chrome.storage.local.set({
[TEMPLATE_KEY]: inputTemplate.value,
});
window.close();
});

class RefTextGenerator {
constructor(doc) {
this.paperTitle =
doc.getElementsByClassName("global-article-title")[0]?.innerHTML ?? "no";
this.paperAuthors =
Array.from(
doc
.getElementsByClassName("global-authors-name-tags")[0]
?.getElementsByTagName("a")
).map((x) => x.innerText) ?? [];
this.journalName =
doc.getElementsByClassName("journal-name")[0]?.innerHTML ?? "no";
const para = doc.getElementsByClassName("global-para")[1]?.innerHTML;
const ySplitted = para.split("年");
this.year = ySplitted[0].trim();
const vSplitted = ySplitted[1].split("巻");
this.volume = vSplitted[0].trim();
const iSplitted = vSplitted[1].split("号");
this.issue = iSplitted[0].trim();
this.page = iSplitted[1].trim();
}

generate(template = DEFAULT_TEMPLATE, authorSeparator = ",") {
const refText = template
.replace("$title", this.paperTitle)
.replace("$authors", this.paperAuthors.join(authorSeparator))
.replace("$journal", this.journalName)
.replace("$year", this.year)
.replace("$volume", this.volume)
.replace("$issue", this.issue)
.replace("$page", this.page);
return refText;
}
}

chrome.tabs.query({ active: true, lastFocusedWindow: true }, async (tabs) => {
let url = tabs[0].url;
const parsed = new URL(url);
const origin = parsed.origin;
const pathname = parsed.pathname;
const paths = pathname
.split("-")[0]
.split("/")
.filter((x) => x.length > 0);
paths.pop();
const newPath = origin + "/" + paths.join("/") + "/_article/-char/ja";
//outView.innerText = newPath;
await fetch(newPath, {
method: "GET",
})
.then(function (response) {
return response.text();
})
.then(function (data) {
const parser = new DOMParser();
const doc = parser.parseFromString(data, "text/html");
const refTextGenerator = new RefTextGenerator(doc);
chrome.storage.local.get([TEMPLATE_KEY], (value) => {
if (value.hasOwnProperty(TEMPLATE_KEY)) {
outView.innerText = refTextGenerator.generate(value[TEMPLATE_KEY]);
} else {
outView.innerText = refTextGenerator.generate(DEFAULT_TEMPLATE);
}
});
});
});

applyButton.addEventListener("click", async () => {
navigator.clipboard.writeText(outView.innerText);
});
Loading

0 comments on commit 891bda4

Please sign in to comment.