Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
elementdavv committed Jul 14, 2023
1 parent f51eafb commit 269c2c6
Show file tree
Hide file tree
Showing 21 changed files with 395 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Taiwan eBook Downloader

Download PDF books from [Taiwan eBook database](https://taiwanebook.ncl.edu.tw).

## Install
For Chrome, Edge, etc:
* Grab the latest version of package(.crx) in the [releases](https://github.com/elementdavv/taiwan_ebook_downloader/releases) page.
* Drop the package from file manager to Extension Manager page of your browser(developer mode must be enabled).

For Firefox:
* Grab the latest version of package(.xpi) in the [releases](https://github.com/elementdavv/taiwan_ebook_downloader/releases) page.
* Drop the package from file manager to Extensions page of your browser.
* Grant Optional permissions from Permissions tab on the extension page.

Alternatively, install automatically from browser extension repositories(not always up to date):

[Chrome WebStore](https://chrome.google.com/webstore/detail/taiwan-ebook-downloader/ealhmnpgajakmajhcdplfnpppdjknpoe) / [Edge Addons](https://microsoftedge.microsoft.com/addons/detail/taiwan-ebook-downloader/ifbmiaibmbefelonkecidnemlcdbllkh) / [Mozilla Addons (Firefox)](https://addons.mozilla.org/en-US/firefox/addon/taiwan_ebook_downloader/)(Optional permissions must be granted as well)

## Usage
In [Taiwan eBook database](https://taiwanebook.ncl.edu.tw) pages, book download buttons will show up automatically.

## License
[GPL3](LICENSE) ©Element Davv

Any questions and/or suggestions are appreciatiated.

## Donation
If you would like to support my development, you can donate some coins. [![donate](resources/pp-logo.png)](https://paypal.me/timelegend)
5 changes: 5 additions & 0 deletions moz/_locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": { "message": "Download PDF ebooks from Taiwan eBook database"}
, "title": { "message": "Taiwan eBook database Downloader" }
, "download": { "message": "Download " }
}
5 changes: 5 additions & 0 deletions moz/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": { "message": "从台湾华文电子书库下载 PDF 电子书"}
, "title": { "message": "台湾华文电子书库下载器" }
, "download": { "message": "下载 " }
}
5 changes: 5 additions & 0 deletions moz/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": { "message": "從臺灣華文電子書庫下載 PDF 電子書"}
, "title": { "message": "臺灣華文電子書庫下載器" }
, "download": { "message": "下載 " }
}
Binary file added moz/images/logo128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions moz/js/bg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* bg.js
* Copyright (C) 2023 Element Davv<[email protected]>
*
* Distributed under terms of the GPL3 license.
*/

(function(){
'use strict';

chrome.action.onClicked.addListener(tab => {
const url = "https://taiwanebook.ncl.edu.tw";
chrome.tabs.create({ url: url })
});

chrome.tabs.onUpdated.addListener((tabid, changeinfo, tab) => {
const re = /https:\/\/taiwanebook.ncl.edu.tw\/(zh-tw|en)\/(category|time|collection|search|book)\/.+/;
const re1 = /reader/;
if (changeinfo.status == 'complete') {
if (!tab.url.match(re) || tab.url.match(re1)) return;
chrome.scripting.executeScript({
files: ['js/content.js']
, target: {tabId: tabid}
});
}
});

})();
14 changes: 14 additions & 0 deletions moz/js/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* content.js
* Copyright (C) 2023 Element Davv<[email protected]>
*
* Distributed under terms of the GPL3 license.
*/

(async () => {
if (typeof window.content1ted === 'undefined' ) {
const src = chrome.runtime.getURL('js/content1.js');
window.content1ted = await import(src);
window.content1ted.default();
}
})();
89 changes: 89 additions & 0 deletions moz/js/content1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* content1.js
* Copyright (C) 2023 Element Davv<[email protected]>
*
* Distributed under terms of the GPL3 license.
*/

export default function(){
'use strict';

function showDownload() {
const items = document.getElementsByClassName('items');
if (items.length > 0) {
const itemlist = items[0].children;
for (var i = 0; i < itemlist.length; i++) {
showForItem(itemlist[i]);
}
}
else {
showForBook();
}
}

async function showForBook() {
const blues = document.getElementsByClassName('ui four column grid');
if (blues.length == 0) return;

const blue = blues[0];
const links = await getDownloadLinks(document.URL);
links.forEach((link, i) => {
addButton(link, blue, 'afterend');
});
}

async function showForItem(item) {
const headers = item.getElementsByClassName('header');
const metas = item.getElementsByClassName('meta');
if (headers.length == 0) return;
if (metas.length == 0) return;

const header = headers[0];
const meta = metas[0];
const href = header.getAttribute('href');
const links = await getDownloadLinks(href);
links.forEach((link, i) => {
addButton(link, meta, 'beforeend');
});
}

const base = 'https://taiwanebook.ncl.edu.tw/';
const classList = ['ui','blue', 'button']

function addButton(link, node, pos) {
const a = document.createElement('a');
a.href = base + link;
const fname = link.substring(link.lastIndexOf('/') + 1)
a.download = fname;
a.textContent = chrome.i18n.getMessage('download') + fname;
classList.forEach((cls, j) => {
a.classList.add(cls);
});
node.insertAdjacentElement(pos, a);
const br = document.createElement('br');
node.insertAdjacentElement(pos, br);
}

async function getDownloadLinks(url) {
const response = await fetch(url+ '/reader');
if (response.ok) {
const text = await response.text();
const re = /ebkFiles.*?PDF/g;
const files = text.match(re);

var links = [];
if (files.length > 1) {
for (var i = 0; i < files.length - 1; i++) {
links.push(files[i]);
}
}
else {
links.push(files[0]);
}
}
return links;
}

console.log('Taiwanebook Downloader v0.1.0 in action');
showDownload();
};
40 changes: 40 additions & 0 deletions moz/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"manifest_version": 3
, "author": "Element Davv <[email protected]>"
, "description": "__MSG_description__"
, "homepage_url": "https://github.com/elementdavv/taiwan_ebook_downloader"
, "name": "Taiwan eBook Downloader"
, "version": "0.1.0"
, "default_locale": "en"
, "browser_specific_settings": {
"gecko": {
"id": "[email protected]"
}
}
, "icons": {
"128": "images/logo128.png"
}
, "action": {
"default_icon": {
"128": "images/logo128.png"
}
, "default_title": "__MSG_title__"
}
, "background": {
"scripts": ["js/bg.js"]
, "type": "module"
}
, "permissions": [
"scripting"
, "tabs"
]
, "host_permissions": [
"https://taiwanebook.ncl.edu.tw/*"
]
, "web_accessible_resources": [{
"resources": [
"js/*"
]
, "matches": ["https://taiwanebook.ncl.edu.tw/*"]
}]
}
Binary file added resources/book1280.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/list1280.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/logo128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/pp-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": { "message": "Download PDF ebooks from Taiwan eBook database"}
, "title": { "message": "Taiwan eBook database Downloader" }
, "download": { "message": "Download " }
}
5 changes: 5 additions & 0 deletions src/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": { "message": "从台湾华文电子书库下载 PDF 电子书"}
, "title": { "message": "台湾华文电子书库下载器" }
, "download": { "message": "下载 " }
}
5 changes: 5 additions & 0 deletions src/_locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"description": { "message": "從臺灣華文電子書庫下載 PDF 電子書"}
, "title": { "message": "臺灣華文電子書庫下載器" }
, "download": { "message": "下載 " }
}
Binary file added src/images/logo128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions src/js/bg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* bg.js
* Copyright (C) 2023 Element Davv<[email protected]>
*
* Distributed under terms of the GPL3 license.
*/

(function(){
'use strict';

chrome.action.onClicked.addListener(tab => {
const url = "https://taiwanebook.ncl.edu.tw";
chrome.tabs.create({ url: url })
});

chrome.tabs.onUpdated.addListener((tabid, changeinfo, tab) => {
const re = /https:\/\/taiwanebook.ncl.edu.tw\/(zh-tw|en)\/(category|time|collection|search|book)\/.+/;
const re1 = /reader/;
if (changeinfo.status == 'complete') {
if (!tab.url.match(re) || tab.url.match(re1)) return;
chrome.scripting.executeScript({
files: ['js/content.js']
, target: {tabId: tabid}
});
}
});

})();
14 changes: 14 additions & 0 deletions src/js/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* content.js
* Copyright (C) 2023 Element Davv<[email protected]>
*
* Distributed under terms of the GPL3 license.
*/

(async () => {
if (typeof window.content1ted === 'undefined' ) {
const src = chrome.runtime.getURL('js/content1.js');
window.content1ted = await import(src);
window.content1ted.default();
}
})();
89 changes: 89 additions & 0 deletions src/js/content1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* content1.js
* Copyright (C) 2023 Element Davv<[email protected]>
*
* Distributed under terms of the GPL3 license.
*/

export default function(){
'use strict';

function showDownload() {
const items = document.getElementsByClassName('items');
if (items.length > 0) {
const itemlist = items[0].children;
for (var i = 0; i < itemlist.length; i++) {
showForItem(itemlist[i]);
}
}
else {
showForBook();
}
}

async function showForBook() {
const blues = document.getElementsByClassName('ui four column grid');
if (blues.length == 0) return;

const blue = blues[0];
const links = await getDownloadLinks(document.URL);
links.forEach((link, i) => {
addButton(link, blue, 'afterend');
});
}

async function showForItem(item) {
const headers = item.getElementsByClassName('header');
const metas = item.getElementsByClassName('meta');
if (headers.length == 0) return;
if (metas.length == 0) return;

const header = headers[0];
const meta = metas[0];
const href = header.getAttribute('href');
const links = await getDownloadLinks(href);
links.forEach((link, i) => {
addButton(link, meta, 'beforeend');
});
}

const base = 'https://taiwanebook.ncl.edu.tw/';
const classList = ['ui','blue', 'button']

function addButton(link, node, pos) {
const a = document.createElement('a');
a.href = base + link;
const fname = link.substring(link.lastIndexOf('/') + 1)
a.download = fname;
a.textContent = chrome.i18n.getMessage('download') + fname;
classList.forEach((cls, j) => {
a.classList.add(cls);
});
node.insertAdjacentElement(pos, a);
const br = document.createElement('br');
node.insertAdjacentElement(pos, br);
}

async function getDownloadLinks(url) {
const response = await fetch(url+ '/reader');
if (response.ok) {
const text = await response.text();
const re = /ebkFiles.*?PDF/g;
const files = text.match(re);

var links = [];
if (files.length > 1) {
for (var i = 0; i < files.length - 1; i++) {
links.push(files[i]);
}
}
else {
links.push(files[0]);
}
}
return links;
}

console.log('Taiwanebook Downloader v0.1.0 in action');
showDownload();
};
Loading

0 comments on commit 269c2c6

Please sign in to comment.