bgm-wiki-rev-diff

show diff between bgm.tv wiki versions

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

  1. // ==UserScript==
  2. // @name bgm-wiki-rev-diff
  3. // @name:zh 显示条目信息版本差异
  4. // @namespace https://trim21.me/
  5. // @version 0.1.2
  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. </style>
  168. `;
  169. function initUI() {
  170. return __awaiter(this, void 0, void 0, function* () {
  171. $('#columnInSubjectA > hr.board').after(style + '<div id="show-trim21-cn"></div>');
  172. const revs = $('#pagehistory li').map(function (e) {
  173. return (0, parser_1.parseRevEl)($(this)).id;
  174. });
  175. $('#pagehistory li').each(function (index) {
  176. const el = $(this);
  177. try {
  178. const rev = (0, parser_1.parseRevEl)(el);
  179. el.prepend(`<input type="radio" class="rev-trim21-cn" name="rev-right" label="select to compare" value="${rev.id}">`);
  180. el.prepend(`<input type="radio" class="rev-trim21-cn" name="rev-left" label="select to compare" value="${rev.id}">`);
  181. const previous = revs[index + 1];
  182. el.prepend(`(<a href="#" data-rev="${rev.id}" data-previous="${previous}" class="l compare-previous-trim21-cn">显示修改</a>) `);
  183. }
  184. catch (e) { }
  185. });
  186. const typeRevert = {
  187. 'rev-left': 'rev-right',
  188. 'rev-right': 'rev-left',
  189. };
  190. $('input[type="radio"]').on('change', function (e) {
  191. const name = e.target.getAttribute('name');
  192. if (!name) {
  193. return;
  194. }
  195. const selectName = typeRevert[name];
  196. const rev = e.target.getAttribute('value');
  197. if (rev) {
  198. $(`input[name="${selectName}"][value="${rev}"]`).attr('disabled', 'disabled');
  199. $(`input[name="${selectName}"][value!="${rev}"]`).attr('disabled', null);
  200. }
  201. });
  202. $('.compare-previous-trim21-cn').on('click', function () {
  203. const el = $(this);
  204. const left = String(el.data('rev'));
  205. const right = String(el.data('previous'));
  206. $('input[name="rev-left"]').attr('checked', null);
  207. $('input[name="rev-right"]').attr('checked', null);
  208. $(`input[name="rev-left"][value="${left}"]`)
  209. .attr('checked', 'true')
  210. .trigger('change');
  211. $(`input[name="rev-right"][value="${right}"]`)
  212. .attr('checked', 'true')
  213. .trigger('change');
  214. (0, compare_1.compare)(left, right);
  215. });
  216. $('#columnInSubjectA span.text').append('<a href="#" id="compare-trim21-cn" class="l"> > 比较选中的版本</a>');
  217. $('#compare-trim21-cn').on('click', function () {
  218. const selectedRevs = getSelectedVersion();
  219. (0, compare_1.compare)(selectedRevs[0], selectedRevs[1]);
  220. });
  221. $('head').append('<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />');
  222. });
  223. }
  224. function getSelectedVersion() {
  225. const selectedVersion = [];
  226. const selectedRev = $('.rev-trim21-cn:checked');
  227. if (selectedRev.length < 2) {
  228. window.alert('请选中两个版本进行比较');
  229. throw new Error();
  230. }
  231. selectedRev.each(function () {
  232. const val = $(this).val();
  233. selectedVersion.push(val);
  234. });
  235. selectedVersion.sort((a, b) => parseInt(b) - parseInt(a));
  236. return selectedVersion;
  237. }
  238. main().catch(console.error);
  239.  
  240.  
  241. /***/ }),
  242.  
  243. /***/ "./src/model.ts":
  244. /***/ (function(__unused_webpack_module, exports) {
  245.  
  246.  
  247. Object.defineProperty(exports, "__esModule", ({ value: true }));
  248. exports.Commit = void 0;
  249. class Commit {
  250. constructor(rev, detail) {
  251. this.rev = rev;
  252. this.details = detail;
  253. }
  254. }
  255. exports.Commit = Commit;
  256.  
  257.  
  258. /***/ }),
  259.  
  260. /***/ "./src/parser.ts":
  261. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  262.  
  263.  
  264. Object.defineProperty(exports, "__esModule", ({ value: true }));
  265. exports.getRevInfo = exports.parseRevEl = exports.parseRevDetails = void 0;
  266. const $ = __webpack_require__("jquery");
  267. function parseRevDetails(html) {
  268. var _a, _b, _c, _d, _e, _f;
  269. const jq = $(html);
  270. const rawInfo = (_b = (_a = jq.find('#subject_infobox').val()) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '';
  271. const title = (_d = (_c = jq.find('input[name="subject_title"]').val()) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : '';
  272. const description = (_f = (_e = jq.find('textarea#subject_summary').val()) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : '';
  273. return { title, rawInfo, description };
  274. }
  275. exports.parseRevDetails = parseRevDetails;
  276. function parseRevEl(el) {
  277. const date = el.find('a:not(.compare-previous-trim21-cn)').first().html();
  278. const revEL = el.find('a.l:contains("恢复")');
  279. const revCommentEl = el.find('span.comment');
  280. let comment = '';
  281. if (revCommentEl.length > 0) {
  282. comment = revCommentEl.html();
  283. comment = comment.substring(1, comment.length - 1);
  284. }
  285. const revHref = revEL.attr('href');
  286. if (!revHref) {
  287. throw new Error();
  288. }
  289. const revID = revHref.split('/').pop();
  290. if (!revID) {
  291. throw new Error(`can't parse rev id from ${revHref}`);
  292. }
  293. return {
  294. id: revID,
  295. comment,
  296. date,
  297. url: revHref,
  298. };
  299. }
  300. exports.parseRevEl = parseRevEl;
  301. function getRevs() {
  302. const revs = [];
  303. $('#pagehistory li').each(function () {
  304. const el = $(this);
  305. try {
  306. revs.push(parseRevEl(el));
  307. }
  308. catch (e) { }
  309. });
  310. return revs;
  311. }
  312. function getRevInfo(revID) {
  313. for (const rev of getRevs()) {
  314. if (rev.id === revID) {
  315. return rev;
  316. }
  317. }
  318. }
  319. exports.getRevInfo = getRevInfo;
  320.  
  321.  
  322. /***/ }),
  323.  
  324. /***/ "./src/ui.ts":
  325. /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
  326.  
  327.  
  328. Object.defineProperty(exports, "__esModule", ({ value: true }));
  329. exports.clear = exports.show = exports.render = void 0;
  330. const Diff2Html = __webpack_require__("diff2html");
  331. const $ = __webpack_require__("jquery");
  332. function render(diff) {
  333. return Diff2Html.html(diff);
  334. }
  335. exports.render = render;
  336. function show(html) {
  337. $('#show-trim21-cn').html(html);
  338. }
  339. exports.show = show;
  340. function clear() {
  341. $('#show-trim21-cn').html('');
  342. }
  343. exports.clear = clear;
  344.  
  345.  
  346. /***/ }),
  347.  
  348. /***/ "jquery":
  349. /***/ (function(module) {
  350.  
  351. module.exports = $;
  352.  
  353. /***/ }),
  354.  
  355. /***/ "diff":
  356. /***/ (function(module) {
  357.  
  358. module.exports = Diff;
  359.  
  360. /***/ }),
  361.  
  362. /***/ "diff2html":
  363. /***/ (function(module) {
  364.  
  365. module.exports = Diff2Html;
  366.  
  367. /***/ })
  368.  
  369. /******/ });
  370. /************************************************************************/
  371. /******/ // The module cache
  372. /******/ var __webpack_module_cache__ = {};
  373. /******/
  374. /******/ // The require function
  375. /******/ function __webpack_require__(moduleId) {
  376. /******/ // Check if module is in cache
  377. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  378. /******/ if (cachedModule !== undefined) {
  379. /******/ return cachedModule.exports;
  380. /******/ }
  381. /******/ // Create a new module (and put it into the cache)
  382. /******/ var module = __webpack_module_cache__[moduleId] = {
  383. /******/ // no module.id needed
  384. /******/ // no module.loaded needed
  385. /******/ exports: {}
  386. /******/ };
  387. /******/
  388. /******/ // Execute the module function
  389. /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  390. /******/
  391. /******/ // Return the exports of the module
  392. /******/ return module.exports;
  393. /******/ }
  394. /******/
  395. /************************************************************************/
  396. /******/
  397. /******/ // startup
  398. /******/ // Load entry module and return exports
  399. /******/ // This entry module is referenced by other modules so it can't be inlined
  400. /******/ var __webpack_exports__ = __webpack_require__("./src/index.ts");
  401. /******/
  402. /******/ })()
  403. ;