diff --git a/README.md b/README.md index e4e018c..25b84f1 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![build](https://github.com/chibat/chrome-extension-typescript-starter/workflows/build/badge.svg) -Chrome Extension, register paper of acmdl, to scrapbox +Chrome Extension, register paper of acmdl and ieee, to scrapbox download: [Chrome webstore](https://chrome.google.com/webstore/detail/scrap-paper/cgkgikddogobbaakbmbjphgipgfbkbdo) @@ -20,12 +20,6 @@ download: [Chrome webstore](https://chrome.google.com/webstore/detail/scrap-pape * Webpack * React * Jest -* Example Code - * Chrome Storage - * Options Version 2 - * content script - * count up badge number - * background ## Project Structure diff --git a/public/manifest.json b/public/manifest.json index 9056554..bc9bed5 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -2,8 +2,8 @@ "manifest_version": 3, "name": "Scrap Paper", - "description": "Chrome Extension, register paper of acmdl, to scrapbox", - "version": "0.2", + "description": "Chrome Extension, register paper of acmdl and ieee, to scrapbox", + "version": "0.4", "options_ui": { "page": "options.html" @@ -15,7 +15,11 @@ "content_scripts": [ { - "matches": ["https://dl.acm.org/doi/*"], + "matches": [ + "https://dl.acm.org/doi/*", + "https://ieeexplore.ieee.org/document/*", + "https://ieeexplore.ieee.org/abstract/document/*" + ], "js": ["js/vendor.js", "js/content_script.js"] } ], diff --git a/src/background.ts b/src/background.ts index 7ae6d4f..6d0a2a3 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,15 +1,16 @@ +var data = { project: "" }; chrome.action.onClicked.addListener((tab: chrome.tabs.Tab) => { chrome.storage.sync.get("project", (value) => { if (tab.id == undefined) { console.log("undefined."); return; } - var proj: string = ""; - proj = value["project"]; - if (proj == "" || proj == undefined) { + data.project = value["project"] ?? ""; + if (data.project == "" || data.project == undefined) { chrome.tabs.create({ url: "options.html" }); } else { - chrome.tabs.sendMessage(tab.id, proj, (response) => {}); + console.log(data); + chrome.tabs.sendMessage(tab.id, data, (response) => {}); } }); }); diff --git a/src/content_script.tsx b/src/content_script.tsx index 7510d03..2b15149 100644 --- a/src/content_script.tsx +++ b/src/content_script.tsx @@ -1,16 +1,16 @@ -chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - registerScrapBox(request); - +chrome.runtime.onMessage.addListener((data, sender, sendResponse) => { + const url: string = location.href; + if (url.includes("acm")) { + registerAcmScrapBox(data.project); + } else if (url.includes("ieee")) { + registerIeeeScrapBox(data.project); + } sendResponse("from content script"); }); -function registerScrapBox(proj: string) { +function registerAcmScrapBox(proj: string) { const url: string = location.href; - const addLink = (text: string) => { - return "[" + text + "]"; - }; - const title_collection = document.getElementsByClassName( "citation__title" ) as HTMLCollectionOf; @@ -64,3 +64,54 @@ function registerScrapBox(proj: string) { body ); } +function registerIeeeScrapBox(proj: string) { + const url: string = location.href; + const title_collection = document.getElementsByClassName( + "document-title" + ) as HTMLCollectionOf; + const title = title_collection[0].children[0].textContent ?? ""; + console.log(title); + + const author_collection = document.getElementsByClassName( + "authors-info-container" + ) as HTMLCollectionOf; + const authors = Array.from(author_collection[0].children).map((e) => { + const author = + e.children[0].children[0].children[0].children[0].textContent; + return addLink(author); + }); + console.log(authors); + const conference_collection = document.getElementsByClassName( + "stats-document-abstract-publishedIn" + ); + const conference = conference_collection[1].textContent; + const abstract = + document.getElementsByClassName("abstract-text")[0].children[0].children[0] + .children[1].textContent; + const lines: string = + url + + "\n" + + addLink(conference) + + " " + + "\n" + + authors.join(", ") + + "\n\n" + + ">" + + abstract + + "\n\n" + + "#survey"; + + var body = encodeURIComponent(lines); + window.open( + "https://scrapbox.io/" + + proj + + "/" + + encodeURIComponent(title.trim()) + + "?body=" + + body + ); +} + +const addLink = (text: string | null) => { + return "[" + text + "]"; +};