ConvertSites.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const URL = require('url').URL;
  2. const ConvertHtml = require('./ConvertHtml');
  3. const sitesFilter = {
  4. 'www.fanfiction.net': {
  5. converter: 'cutter',
  6. begin: `<div class='storytext xcontrast_txt nocopy' id='storytext'>`,
  7. end: `<div style='height:5px'></div><div style='clear:both;text-align:right;'>`,
  8. },
  9. 'archiveofourown.org': {
  10. converter: 'cutter',
  11. begin: `<!-- BEGIN section where work skin applies -->`,
  12. end: `<!-- END work skin -->`,
  13. }
  14. };
  15. class ConvertSites extends ConvertHtml {
  16. check(data, opts) {
  17. const {url, dataType} = opts;
  18. const parsedUrl = new URL(url);
  19. if (dataType && dataType.ext == 'html') {
  20. if (sitesFilter[parsedUrl.hostname])
  21. return {hostname: parsedUrl.hostname};
  22. }
  23. return false;
  24. }
  25. async run(data, opts) {
  26. if (!opts.enableSitesFilter)
  27. return false;
  28. const checkResult = this.check(data, opts);
  29. if (!checkResult)
  30. return false;
  31. const {hostname} = checkResult;
  32. let text = this.decode(data).toString();
  33. text = this[sitesFilter[hostname].converter](text, sitesFilter[hostname]);
  34. if (text === false)
  35. return false;
  36. return await super.run(Buffer.from(text), {skipCheck: true, cutTitle: true});
  37. }
  38. getTitle(text) {
  39. let title = '';
  40. const m = text.match(/<title>([\s\S]*?)<\/title>/);
  41. if (m)
  42. title = m[1];
  43. return `<title>${title.trim()}</title>`;
  44. }
  45. cutter(text, opts) {
  46. const title = this.getTitle(text);
  47. const l = text.indexOf(opts.begin) + opts.begin.length;
  48. const r = text.indexOf(opts.end);
  49. if (l < 0 || r < 0 || r <= l)
  50. return false;
  51. return text.substring(l, r) + title;
  52. }
  53. }
  54. module.exports = ConvertSites;