Library Helper

A userscript that display links between different libraries and book stores.

当前为 2020-04-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Library Helper
  3. // @namespace https://github.com/chihchun
  4. // @version 1.8
  5. // @description A userscript that display links between different libraries and book stores.
  6. // @author Rex Tsai <rex.cc.tsai@gmail.com>
  7. // @match http://book.tpml.edu.tw/webpac/bookDetail.do*
  8. // @match https://book.tpml.edu.tw/webpac/bookDetail.do*
  9. // @match https://book.douban.com/subject/*
  10. // @match https://books.google.com.tw/books*
  11. // @match https://books.google.com/books*
  12. // @match https://books.google.fr/books*
  13. // @match https://play.google.com/store/books/details/*
  14. // @match https://play.google.com/store/books/details?id=*
  15. // @match https://share.readmoo.com/book/*
  16. // @match https://readmoo.com/book/*
  17. // @match https://www.amazon.cn/dp/*
  18. // @match https://www.amazon.cn/gp/product/*
  19. // @match https://www.amazon.com/*/dp/*
  20. // @match https://www.amazon.com/gp/product/*
  21. // @match https://www.books.com.tw/products/*
  22. // @match https://www.goodreads.com/book/show/*
  23. // @match https://www.kobo.com/*/ebook*
  24. // @match https://www.taaze.tw/goods/*
  25. // @match https://www.taaze.tw/usedList.html?oid=*
  26. // @match https://www.babelio.com/livres/*
  27. // @match http://bibliotheque.ville-bobigny.fr/detail-d-une-notice/notice/*
  28. // @match https://webpac.tphcc.gov.tw/webpac/content.cfm*
  29. // @grant none
  30. // @require https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.13.1/js-yaml.min.js
  31. // @run-at document-idle
  32. // @license MIT; https://github.com/chihchun/library-helper/blob/master/LICENSE
  33. // @copyright 2019, chihchun (https://github.com/chihchun)
  34. // @supportURL https://github.com/chihchun/library-helper/issues
  35. // ==/UserScript==
  36.  
  37. (function() {
  38. 'use strict';
  39.  
  40. var metadata_yaml = `
  41. amazon.com:
  42. matches:
  43. - https://www.amazon.com/gp/product/*
  44. type: 'XPATH'
  45. metadata:
  46. title: "//span[@id='ebooksProductTitle']"
  47. authors: "//span[contains(@data-a-popover, 'Author Dialog')]/a"
  48. asin: "//form[@id='sendSample']/input[@name='ASIN.0']/@value"
  49. amazon.com/dp:
  50. matches:
  51. - https://www.amazon.com/*
  52. type: 'XPATH'
  53. metadata:
  54. title: "//span[@id='ebooksProductTitle']"
  55. authors: //span[contains(@class, 'author')]/a
  56. asin: "//form[@id='sendSample']/input[@name='ASIN.0']/@value"
  57.  
  58. amazon.cn:
  59. matches:
  60. - "https://www.amazon.cn/gp/product/*"
  61. - "https://www.amazon.cn/dp/*"
  62. type: 'XPATH'
  63. metadata:
  64. title: "//span[@id='ebooksProductTitle']"
  65. authors: "//span[contains(@class,'author')]/a"
  66. asin: "//form[@id='sendSample']/input[@name='ASIN.0']/@value"
  67.  
  68. babelio.com:
  69. matches:
  70. - "https://www.babelio.com/livres/*"
  71. type: 'XPATH'
  72. metadata:
  73. title: "//h1"
  74. authors: "//span[@itemprop='name']"
  75.  
  76. bibliotheque.ville-bobigny.fr:
  77. matches:
  78. - "http://bibliotheque.ville-bobigny.fr/detail-d-une-notice/notice/*"
  79. type: 'XPATH'
  80. metadata:
  81. title: "//title"
  82. authros: "//a[contains(@class, 'ntc-link-auteur')]"
  83.  
  84. books.com.tw:
  85. matches:
  86. - "https://www.books.com.tw/products/*"
  87. type: 'XPATH'
  88. metadata:
  89. title: "//h1"
  90. origtitle: "//h2/a[contains(@href,'https://search.books.com.tw/search/query/cat/all/key')]"
  91. isbn: "//li[contains(text(),'ISBN')]"
  92. price: "//ul[@class='price']/li/em"
  93. sellingprice: "//b[@itemprop='price']"
  94. authors: "//a[contains(@href,'adv_author')]"
  95. publishdate: "//li[contains(text(),'出版日期')]"
  96.  
  97. books.google.com.tw:
  98. matches:
  99. - "https://books.google.com.tw/books/*"
  100. - "https://books.google.com/books/*"
  101. - "https://books.google.fr/books/*"
  102. type: 'XPATH'
  103. metadata:
  104. title: "//meta[@property='og:title']/@content"
  105. authors: "//a[contains(@href,'q=inauthor')]"
  106.  
  107. goodreads.com:
  108. matches:
  109. - "https://www.goodreads.com/book/show/*"
  110. type: 'XPATH'
  111. metadata:
  112. title: "//meta[@property='og:title']/@content"
  113. authors: "//a[@class='authorName']/span[@itemprop='name']"
  114. isbn: "//meta[@property='books:isbn']/@content"
  115. rating: "//span[@itemprop='ratingValue']"
  116.  
  117. kobo.com:
  118. matches:
  119. - "https://www.kobo.com/*"
  120. type: 'JSON-LD'
  121. metadata:
  122. title: '//span[@class="title product-field"]'
  123. authors: '//a[@class="contributor-name"]'
  124. origtitle: "//span[contains(@class, 'subtitle')]"
  125.  
  126. play.google.com:
  127. matches:
  128. - "https://play.google.com/store/books/details/*"
  129. - "https://play.google.com/store/books/details?id=*"
  130. type: 'JSON-LD'
  131. metadata:
  132.  
  133. readmoo.com:
  134. matches:
  135. - "https://share.readmoo.com/book/*"
  136. - "https://readmoo.com/book/*"
  137. type: 'XPATH'
  138. metadata:
  139. title: "//h2"
  140. isbn: "//span[@itemprop='ISBN']"
  141. authors: "//span[@itemprop='name']/a"
  142.  
  143. taaze.tw:
  144. matches:
  145. - "https://www.taaze.tw/goods/*"
  146. type: 'XPATH'
  147. metadata:
  148. title: "//div[contains(@class, 'mBody')]//h1"
  149. origtitle: "//div[contains(@class, 'mBody')]//h2"
  150. isbn: "//meta[@property='books:isbn']/@content"
  151. authors: "//div[@class='authorBrand']//a[contains(@href,'rwd_searchResult.html?keyType%5B%5D=2')]"
  152. taaze.tw/used:
  153. matches:
  154. - "https://www.taaze.tw/usedList.html*"
  155. type: 'XPATH'
  156. metadata:
  157. origtitle: "//div[contains(@class, 'hide')]//div[@class='title-next']"
  158. authors: "//a[contains(@href,'rwd_searchResult.html?keyType%5B%5D=2')]"
  159.  
  160. tpml.edu.tw:
  161. matches:
  162. - "http://book.tpml.edu.tw/webpac/bookDetail.do*"
  163. - "https://book.tpml.edu.tw/webpac/bookDetail.do*"
  164. type: 'XPATH'
  165. metadata:
  166. title: "//h3"
  167. authors: "//a[contains(@href,'search_field=PN')]"
  168.  
  169. webpac.tphcc.gov.tw:
  170. matches:
  171. - "https://webpac.tphcc.gov.tw/webpac/content.cfm*"
  172. type: 'XPATH'
  173. metadata:
  174. title: "//h2"
  175. authors: "//div[@class='detail simple']/p[1]"
  176. isbn: "//div[@class='detail simple']/p[4]"
  177.  
  178. `;
  179.  
  180. var search_yaml = `
  181. 博客來:
  182. url: "https://search.books.com.tw/search/query/key/"
  183. languages:
  184. - "en"
  185. - "en-US"
  186. - "zh"
  187. - "zh-TW"
  188. - "zh-HK"
  189. - "zh-CN"
  190.  
  191. Kobo:
  192. url: "https://www.kobo.com/tw/zh/search?query="
  193. languages:
  194. - "en"
  195. - "en-US"
  196. - "zh"
  197. - "zh-TW"
  198. - "zh-HK"
  199. - "zh-CN"
  200. - "fr"
  201. - "fr-ca"
  202. - "fr-fr"
  203.  
  204. GooglePlay:
  205. url: "https://play.google.com/store/search?c=books&q="
  206. languages:
  207. - "en"
  208. - "en-US"
  209. - "zh"
  210. - "zh-TW"
  211. - "zh-HK"
  212. - "zh-CN"
  213. - "fr"
  214. - "fr-ca"
  215. - "fr-fr"
  216.  
  217. AmazonCN:
  218. url: "https://www.amazon.cn/s?rh=n%3A116169071&k="
  219. languages:
  220. - "en"
  221. - "en-US"
  222. - "zh"
  223. - "zh-TW"
  224. - "zh-HK"
  225. - "zh-CN"
  226.  
  227. 豆瓣:
  228. url: "https://search.douban.com/book/subject_search?search_text="
  229. languages:
  230. - "zh"
  231. - "zh-TW"
  232. - "zh-HK"
  233. - "zh-CN"
  234.  
  235. Goodreads:
  236. url: "https://www.goodreads.com/search?q="
  237. languages:
  238. - "en"
  239. - "en-US"
  240. - "zh"
  241. - "zh-TW"
  242. - "zh-HK"
  243. - "zh-CN"
  244. - "fr"
  245. - "fr-ca"
  246. - "fr-fr"
  247.  
  248. Google:
  249. url: "https://www.google.com/search?tbm=bks&q="
  250. languages:
  251. - "en"
  252. - "en-US"
  253. - "zh"
  254. - "zh-TW"
  255. - "zh-HK"
  256. - "zh-CN"
  257. - "fr"
  258. - "fr-ca"
  259. - "fr-fr"
  260.  
  261. 北市圖書館:
  262. url: "https://book.tpml.edu.tw/webpac/bookSearchList.do?search_field=FullText&search_input="
  263. languages:
  264. - "en"
  265. - "en-US"
  266. - "zh"
  267. - "zh-TW"
  268. - "zh-HK"
  269. - "zh-CN"
  270.  
  271. 北市圖書館Hyread:
  272. url: "https://tpml.ebook.hyread.com.tw/searchList.jsp?search_field=FullText&search_input="
  273. languages:
  274. - "zh"
  275. - "zh-TW"
  276. - "zh-HK"
  277.  
  278. 新北市圖書館:
  279. url: "https://webpac.tphcc.gov.tw/webpac/search.cfm?m=ss&t0=k&c0=and&k0="
  280. languages:
  281. - "en"
  282. - "en-US"
  283. - "zh"
  284. - "zh-TW"
  285. - "zh-HK"
  286. - "zh-CN"
  287.  
  288. 新北市Hyread:
  289. url: "https://tphcc.ebook.hyread.com.tw/searchList.jsp?search_field=FullText&search_input="
  290. languages:
  291. - "zh"
  292. - "zh-TW"
  293. - "zh-HK"
  294.  
  295. 國立臺灣圖書館Hyread:
  296. url: "https://ntledu.ebook.hyread.com.tw/searchList.jsp?search_field=FullText&search_input="
  297. languages:
  298. - "zh"
  299. - "zh-TW"
  300. - "zh-HK"
  301.  
  302. 台灣雲端書庫:
  303. url: "https://www.ebookservice.tw/#search/"
  304. languages:
  305. - "zh"
  306. - "zh-TW"
  307. - "zh-HK"
  308.  
  309. 讀冊:
  310. url: "https://www.taaze.tw/rwd_searchResult.html?keyword%5B%5D="
  311. languages:
  312. - "zh"
  313. - "zh-TW"
  314. - "zh-HK"
  315. - "zh-CN"
  316.  
  317. Readmoo:
  318. url: "https://share.readmoo.com/search/keyword?q="
  319. languages:
  320. - "zh"
  321. - "zh-TW"
  322. - "zh-HK"
  323. - "zh-CN"
  324.  
  325. Amazon:
  326. url: "https://www.amazon.com/s?i=digital-text&k="
  327. languages:
  328. - "en"
  329. - "en-US"
  330. - "zh"
  331. - "zh-TW"
  332. - "zh-HK"
  333. - "zh-CN"
  334. - "fr"
  335. - "fr-ca"
  336. - "fr-fr"
  337.  
  338. Babelio:
  339. url: "https://www.babelio.com/resrecherche.php?Recherche="
  340. languages:
  341. - "fr"
  342. - "fr-ca"
  343. - "fr-fr"
  344.  
  345. Bobigny:
  346. url: "http://bibliotheque.ville-bobigny.fr/recherche-catalogue/recherche-simple/simple/Mots%20Notice/0/"
  347. languages:
  348. - "fr"
  349. - "fr-ca"
  350. - "fr-fr"
  351. `;
  352.  
  353. var keywords = ['title', 'authors', 'origtitle', 'isbn', 'asin'];
  354.  
  355.  
  356. parse_metadata();
  357.  
  358. function parse_metadata() {
  359. var rules = jsyaml.load(metadata_yaml);
  360. var data = {};
  361.  
  362. // parse the ld+json
  363. var jsons = evaluate('//script[@type="application/ld+json"]');
  364. if(jsons.length > 0) {
  365. jsons.forEach(function(json) {
  366. var ld = JSON.parse(json);
  367. console.debug(ld);
  368. if(ld['@type'] == "Book") {
  369. data['title'] = [ld['name']];
  370.  
  371. if(ld['isbn'] != undefined) {
  372. data['isbn']= [ld['isbn']];
  373. }
  374.  
  375. if(ld['workExample'] != undefined && ld['workExample']['isbn'] != undefined ) {
  376. data['isbn']= [ld['workExample']['isbn']];
  377. }
  378.  
  379. data['authors'] = [];
  380. if(ld['author'] != undefined) {
  381. if(Array.isArray(ld['author'])) {
  382. ld['author'].forEach(function (author) {
  383. data['authors'].push(author['name']);
  384. })
  385. } else {
  386. data['authors'].push(ld['author']['name']);
  387. }
  388. }
  389. }
  390. })
  391. }
  392.  
  393. // parse the metadata by xpath
  394. for (var domain in rules) {
  395. rules[domain]['matches'].forEach(function (match) {
  396. if(document.URL.match(match)) {
  397. var metadata = rules[domain]['metadata'];
  398. for (var key in metadata) {
  399. data[key] = evaluate(metadata[key]);
  400. }
  401. return;
  402. }
  403. })
  404. }
  405.  
  406. // Links to other websites
  407. if(Object.keys(data).length > 0) {
  408. console.debug(data);
  409. var dialog = inject();
  410. var urlsforsearch = jsyaml.load(search_yaml);
  411.  
  412. for (var service in urlsforsearch) {
  413. if(!isPreferLang(urlsforsearch[service]['languages'])) {
  414. continue;
  415. }
  416.  
  417. var url = urlsforsearch[service]['url'];
  418. var html = `<div>${service}: `;
  419. keywords.forEach(function(key) {
  420. if(data[key] != undefined) {
  421. data[key].forEach(function(val) {
  422. var href = url + encodeURI(val);
  423. html += `<a href="${href}" target="_blank">${val}</a> `;
  424. })
  425. }
  426. })
  427. html += "</div>";
  428. dialog.insertAdjacentHTML('beforeend', html)
  429. }
  430. }
  431. }
  432.  
  433. function isPreferLang(offers) {
  434. var languages = window.navigator.userLanguage || window.navigator.languages || [window.navigator.language];
  435. var ret = false;
  436. languages.forEach(function(lang) {
  437. if(offers.includes(lang)) {
  438. ret = true;
  439. }
  440. })
  441. return ret;
  442. }
  443.  
  444. function inject () {
  445. var div = document.createElement('div');
  446. div.id = "libraryhelper";
  447. div.className = "libraryhelper";
  448. div.textContent = 'Library helper';
  449. // Make the DIV element draggable:
  450. dragElement(div);
  451. document.body.appendChild(div);
  452.  
  453. var style = document.createElement('style');
  454. style.innerHTML = `
  455. div.libraryhelper {
  456. color: blueviolet;
  457. border: 1px solid #d3d3d3;
  458. background-color: rgba(255, 255, 255, 0.6);
  459.  
  460. position: fixed;
  461. top: 150px;
  462. right: 0px;
  463.  
  464. width: 30vw;
  465. max-height: 50vh;
  466. padding: 10px;
  467.  
  468. overflow-x: scroll;
  469. cursor: move;
  470. z-index: 9999;
  471. }
  472. `;
  473. document.head.appendChild(style);
  474. return div;
  475. }
  476.  
  477.  
  478. function dragElement(elmnt) {
  479. var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  480. if (document.getElementById(elmnt.id + "header")) {
  481. // if present, the header is where you move the DIV from:
  482. document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  483. } else {
  484. // otherwise, move the DIV from anywhere inside the DIV:
  485. elmnt.onmousedown = dragMouseDown;
  486. }
  487.  
  488. function dragMouseDown(e) {
  489. e = e || window.event;
  490. e.preventDefault();
  491. // get the mouse cursor position at startup:
  492. pos3 = e.clientX;
  493. pos4 = e.clientY;
  494. document.onmouseup = closeDragElement;
  495. // call a function whenever the cursor moves:
  496. document.onmousemove = elementDrag;
  497. }
  498.  
  499. function elementDrag(e) {
  500. e = e || window.event;
  501. e.preventDefault();
  502. // calculate the new cursor position:
  503. pos1 = pos3 - e.clientX;
  504. pos2 = pos4 - e.clientY;
  505. pos3 = e.clientX;
  506. pos4 = e.clientY;
  507. // set the element's new position:
  508. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  509. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  510. }
  511.  
  512. function closeDragElement() {
  513. // stop moving when mouse button is released:
  514. document.onmouseup = null;
  515. document.onmousemove = null;
  516. }
  517. }
  518.  
  519. function evaluate(xpath, doc = document.documentElement) {
  520. var evaluator = new XPathEvaluator();
  521. var result = evaluator.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
  522. var node = undefined;
  523. var texts = [];
  524.  
  525. while(node = result.iterateNext()) {
  526. var text;
  527.  
  528. if (node instanceof Attr) {
  529. text = node.value
  530. } else {
  531. text = node.innerText;
  532. }
  533. if(text == undefined || text == 'null') {
  534. console.error(xpath + " not found on " + document.URL);
  535. continue;
  536. }
  537. // fixing up content
  538. text = text.replace("ISBN:", "").
  539. replace("作者 :", "").
  540. replace("出版日期:", "");
  541. const subtitles = text.split(':');
  542. if(subtitles.length > 1) {
  543. texts.push(subtitles[0]);
  544. }
  545. texts.push(text);
  546. }
  547. return texts;
  548. }
  549. })();
  550.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址