bgm-wiki-rev-diff

show diff between bgm.tv wiki versions

当前为 2021-09-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name bgm-wiki-rev-diff
  3. // @name:zh 显示条目信息版本差异
  4. // @namespace https://trim21.me/
  5. // @version 0.1.4
  6. // @author Trim21 <i@trim21.me>
  7. // @source https://github.com/Trim21/bgm-wiki-rev-diff
  8. // @supportURL https://github.com/Trim21/bgm-wiki-rev-diff/issues
  9. // @license MIT
  10. // @match https://bgm.tv/subject/*/edit*
  11. // @match https://bangumi.tv/subject/*/edit*
  12. // @match https://chii.in/subject/*/edit*
  13. // @require https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js
  14. // @require https://cdn.jsdelivr.net/npm/diff2html@3.4.11/bundles/js/diff2html.min.js
  15. // @require https://cdn.jsdelivr.net/npm/diff@5.0.0/dist/diff.min.js
  16. // @connect bgm.tv
  17. // @connect bangumi.tv
  18. // @run-at document-end
  19. // @description show diff between bgm.tv wiki versions
  20. // ==/UserScript==
  21.  
  22.  
  23. /******/ (function() { // webpackBootstrap
  24. /******/ "use strict";
  25. /******/ var __webpack_modules__ = ({
  26.  
  27. /***/ "./src/compare.ts":
  28. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  29.  
  30.  
  31. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  32. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  33. return new (P || (P = Promise))(function (resolve, reject) {
  34. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  35. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  36. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  37. step((generator = generator.apply(thisArg, _arguments || [])).next());
  38. });
  39. };
  40. Object.defineProperty(exports, "__esModule", ({ value: true }));
  41. exports.compare = void 0;
  42. const parser_1 = __webpack_require__("./src/parser.ts");
  43. const differ_1 = __webpack_require__("./src/differ.ts");
  44. const ui_1 = __webpack_require__("./src/ui.ts");
  45. const model_1 = __webpack_require__("./src/model.ts");
  46. function compare(revID1, revID2) {
  47. (0, ui_1.clear)();
  48. (0, ui_1.show)('<h2>loading versions...</h2>');
  49. const rev1 = (0, parser_1.getRevInfo)(revID1);
  50. const rev2 = (0, parser_1.getRevInfo)(revID2);
  51. if (!rev1) {
  52. throw new Error(`error finding ${revID1}`);
  53. }
  54. const ps = [fetchRev(rev1), fetchRev(rev2)];
  55. Promise.all(ps)
  56. .then((values) => {
  57. const d = (0, differ_1.diff)(values[1], values[0]);
  58. const rendered = (0, ui_1.render)(d);
  59. return (0, ui_1.show)(rendered);
  60. })
  61. .catch((e) => {
  62. console.log(e);
  63. (0, ui_1.show)('<h2 style="color:red">loading versions error</h2>');
  64. });
  65. }
  66. exports.compare = compare;
  67. const _cache = {};
  68. function fetchRev(rev) {
  69. return __awaiter(this, void 0, void 0, function* () {
  70. if (!rev) {
  71. return new model_1.Commit({
  72. id: '0',
  73. comment: '',
  74. date: '',
  75. url: '',
  76. }, {
  77. title: '',
  78. rawInfo: '',
  79. description: '',
  80. });
  81. }
  82. if (!_cache[rev.id]) {
  83. const res = yield fetch(rev.url);
  84. _cache[rev.id] = new model_1.Commit(rev, (0, parser_1.parseRevDetails)(yield res.text()));
  85. }
  86. return _cache[rev.id];
  87. });
  88. }
  89.  
  90.  
  91. /***/ }),
  92.  
  93. /***/ "./src/differ.ts":
  94. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  95.  
  96.  
  97. Object.defineProperty(exports, "__esModule", ({ value: true }));
  98. exports.diff = void 0;
  99. const Diff = __webpack_require__("diff");
  100. function diff(revOld, revNew) {
  101. console.log(revOld);
  102. console.log(revNew);
  103. const d = [
  104. titleDiff(revOld, revNew),
  105. infoDiff(revOld, revNew),
  106. descriptionDiff(revOld, revNew),
  107. ].join('\n');
  108. console.log(d);
  109. return d;
  110. }
  111. exports.diff = diff;
  112. function titleDiff(rev1, rev2) {
  113. if (rev1.details.title === rev2.details.title) {
  114. return '';
  115. }
  116. return Diff.createPatch('条目名', rev1.details.title, rev2.details.title, rev1.rev.date, rev2.rev.date);
  117. }
  118. function infoDiff(rev1, rev2) {
  119. if (rev1.details.rawInfo === rev2.details.rawInfo) {
  120. return '';
  121. }
  122. return Diff.createPatch('相关信息', rev1.details.rawInfo, rev2.details.rawInfo, rev1.rev.date, rev2.rev.date);
  123. }
  124. function descriptionDiff(rev1, rev2) {
  125. if (rev1.details.description === rev2.details.description) {
  126. return '';
  127. }
  128. return Diff.createPatch('简介', rev1.details.description, rev2.details.description, rev1.rev.date, rev2.rev.date);
  129. }
  130.  
  131.  
  132. /***/ }),
  133.  
  134. /***/ "./src/index.ts":
  135. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  136.  
  137.  
  138. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  139. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  140. return new (P || (P = Promise))(function (resolve, reject) {
  141. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  142. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  143. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  144. step((generator = generator.apply(thisArg, _arguments || [])).next());
  145. });
  146. };
  147. Object.defineProperty(exports, "__esModule", ({ value: true }));
  148. const $ = __webpack_require__("jquery");
  149. const parser_1 = __webpack_require__("./src/parser.ts");
  150. const compare_1 = __webpack_require__("./src/compare.ts");
  151. function main() {
  152. return __awaiter(this, void 0, void 0, function* () {
  153. console.log('start bgm-wiki-rev-diff UserScript');
  154. yield initUI();
  155. });
  156. }
  157. const style = `
  158. <style>
  159. #show-trim21-cn .d2h-code-line {
  160. width: calc(100% - 8em);
  161. padding-right: 0;
  162. }
  163.  
  164. #show-trim21-cn .d2h-code-line-ctn {
  165. width: calc(100% - 8em);
  166. }
  167.  
  168. #columnInSubjectA .rev-trim21-cn {
  169. margin: 0 0.2em;
  170. }
  171.  
  172. ul#pagehistory > li > * {
  173. vertical-align: middle;
  174. }
  175. </style>
  176. `;
  177. function initUI() {
  178. return __awaiter(this, void 0, void 0, function* () {
  179. $('#columnInSubjectA > hr.board').after(style + '<div id="show-trim21-cn"></div>');
  180. const revs = $('#pagehistory li').map(function (e) {
  181. return (0, parser_1.parseRevEl)($(this)).id;
  182. });
  183. $('#pagehistory li').each(function (index) {
  184. const el = $(this);
  185. try {
  186. const rev = (0, parser_1.parseRevEl)(el);
  187. el.prepend(`<input type="radio" class="rev-trim21-cn" name="rev-right" label="select to compare" value="${rev.id}">`);
  188. el.prepend(`<input type="radio" class="rev-trim21-cn" name="rev-left" label="select to compare" value="${rev.id}">`);
  189. const previous = revs[index + 1];
  190. el.prepend(`(<a href="#" data-rev="${rev.id}" data-previous="${previous}" class="l compare-previous-trim21-cn">显示修改</a>) `);
  191. }
  192. catch (e) { }
  193. });
  194. const typeRevert = {
  195. 'rev-left': 'rev-right',
  196. 'rev-right': 'rev-left',
  197. };
  198. $('input[type="radio"]').on('change', function (e) {
  199. const name = e.target.getAttribute('name');
  200. if (!name) {
  201. return;
  202. }
  203. const selectName = typeRevert[name];
  204. const rev = e.target.getAttribute('value');
  205. if (rev) {
  206. $(`input[name="${selectName}"][value="${rev}"]`).css('visibility', 'hidden');
  207. $(`input[name="${selectName}"][value!="${rev}"]`).css('visibility', 'visible');
  208. }
  209. });
  210. $('.compare-previous-trim21-cn').on('click', function () {
  211. const el = $(this);
  212. const left = String(el.data('rev'));
  213. const right = String(el.data('previous'));
  214. $('input[name="rev-left"]').attr('checked', null);
  215. $(`input[name="rev-left"][value="${left}"]`)
  216. .attr('checked', 'true')
  217. .trigger('change');
  218. $('input[name="rev-right"]').attr('checked', null);
  219. $(`input[name="rev-right"][value="${right}"]`)
  220. .attr('checked', 'true')
  221. .trigger('change');
  222. (0, compare_1.compare)(left, right);
  223. });
  224. $('#columnInSubjectA span.text').append('<a href="#" id="compare-trim21-cn" class="l"> > 比较选中的版本</a>');
  225. $('#compare-trim21-cn').on('click', function () {
  226. const selectedRevs = getSelectedVersion();
  227. (0, compare_1.compare)(selectedRevs[0], selectedRevs[1]);
  228. });
  229. $('head').append('<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />');
  230. });
  231. }
  232. function getSelectedVersion() {
  233. const selectedVersion = [];
  234. const selectedRev = $('.rev-trim21-cn:checked');
  235. if (selectedRev.length < 2) {
  236. window.alert('请选中两个版本进行比较');
  237. throw new Error();
  238. }
  239. selectedRev.each(function () {
  240. const val = $(this).val();
  241. selectedVersion.push(val);
  242. });
  243. selectedVersion.sort((a, b) => parseInt(b) - parseInt(a));
  244. return selectedVersion;
  245. }
  246. main().catch(console.error);
  247.  
  248.  
  249. /***/ }),
  250.  
  251. /***/ "./src/model.ts":
  252. /***/ (function(__unused_webpack_module, exports) {
  253.  
  254.  
  255. Object.defineProperty(exports, "__esModule", ({ value: true }));
  256. exports.Commit = void 0;
  257. class Commit {
  258. constructor(rev, detail) {
  259. this.rev = rev;
  260. this.details = detail;
  261. }
  262. }
  263. exports.Commit = Commit;
  264.  
  265.  
  266. /***/ }),
  267.  
  268. /***/ "./src/parser.ts":
  269. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  270.  
  271.  
  272. Object.defineProperty(exports, "__esModule", ({ value: true }));
  273. exports.getRevInfo = exports.parseRevEl = exports.parseRevDetails = void 0;
  274. const $ = __webpack_require__("jquery");
  275. function parseRevDetails(html) {
  276. var _a, _b, _c, _d, _e, _f;
  277. const jq = $(html);
  278. const rawInfo = (_b = (_a = jq.find('#subject_infobox').val()) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '';
  279. const title = (_d = (_c = jq.find('input[name="subject_title"]').val()) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : '';
  280. const description = (_f = (_e = jq.find('textarea#subject_summary').val()) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : '';
  281. return {
  282. title,
  283. rawInfo,
  284. description,
  285. };
  286. }
  287. exports.parseRevDetails = parseRevDetails;
  288. function parseRevEl(el) {
  289. const date = el.find('a:not(.compare-previous-trim21-cn)').first().html();
  290. const revEL = el.find('a.l:contains("恢复")');
  291. const revCommentEl = el.find('span.comment');
  292. let comment = '';
  293. if (revCommentEl.length > 0) {
  294. comment = revCommentEl.html();
  295. comment = comment.substring(1, comment.length - 1);
  296. }
  297. const revHref = revEL.attr('href');
  298. if (!revHref) {
  299. throw new Error();
  300. }
  301. const revID = revHref.split('/').pop();
  302. if (!revID) {
  303. throw new Error(`can't parse rev id from ${revHref}`);
  304. }
  305. return {
  306. id: revID,
  307. comment,
  308. date,
  309. url: revHref,
  310. };
  311. }
  312. exports.parseRevEl = parseRevEl;
  313. function getRevs() {
  314. const revs = [];
  315. $('#pagehistory li').each(function () {
  316. const el = $(this);
  317. try {
  318. revs.push(parseRevEl(el));
  319. }
  320. catch (e) { }
  321. });
  322. return revs;
  323. }
  324. function getRevInfo(revID) {
  325. for (const rev of getRevs()) {
  326. if (rev.id === revID) {
  327. return rev;
  328. }
  329. }
  330. }
  331. exports.getRevInfo = getRevInfo;
  332.  
  333.  
  334. /***/ }),
  335.  
  336. /***/ "./src/ui.ts":
  337. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  338.  
  339.  
  340. Object.defineProperty(exports, "__esModule", ({ value: true }));
  341. exports.clear = exports.show = exports.render = void 0;
  342. const Diff2Html = __webpack_require__("diff2html");
  343. const $ = __webpack_require__("jquery");
  344. function render(diff) {
  345. return Diff2Html.html(diff);
  346. }
  347. exports.render = render;
  348. function show(html) {
  349. $('#show-trim21-cn').html(html);
  350. }
  351. exports.show = show;
  352. function clear() {
  353. $('#show-trim21-cn').html('');
  354. }
  355. exports.clear = clear;
  356.  
  357.  
  358. /***/ }),
  359.  
  360. /***/ "jquery":
  361. /***/ (function(module) {
  362.  
  363. module.exports = $;
  364.  
  365. /***/ }),
  366.  
  367. /***/ "diff":
  368. /***/ (function(module) {
  369.  
  370. module.exports = Diff;
  371.  
  372. /***/ }),
  373.  
  374. /***/ "diff2html":
  375. /***/ (function(module) {
  376.  
  377. module.exports = Diff2Html;
  378.  
  379. /***/ })
  380.  
  381. /******/ });
  382. /************************************************************************/
  383. /******/ // The module cache
  384. /******/ var __webpack_module_cache__ = {};
  385. /******/
  386. /******/ // The require function
  387. /******/ function __webpack_require__(moduleId) {
  388. /******/ // Check if module is in cache
  389. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  390. /******/ if (cachedModule !== undefined) {
  391. /******/ return cachedModule.exports;
  392. /******/ }
  393. /******/ // Create a new module (and put it into the cache)
  394. /******/ var module = __webpack_module_cache__[moduleId] = {
  395. /******/ // no module.id needed
  396. /******/ // no module.loaded needed
  397. /******/ exports: {}
  398. /******/ };
  399. /******/
  400. /******/ // Execute the module function
  401. /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  402. /******/
  403. /******/ // Return the exports of the module
  404. /******/ return module.exports;
  405. /******/ }
  406. /******/
  407. /************************************************************************/
  408. /******/
  409. /******/ // startup
  410. /******/ // Load entry module and return exports
  411. /******/ // This entry module is referenced by other modules so it can't be inlined
  412. /******/ var __webpack_exports__ = __webpack_require__("./src/index.ts");
  413. /******/
  414. /******/ })()
  415. ;