Jira History Comparison

View Jira History word by word

  1. // ==UserScript==
  2. // @name Jira History Comparison
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description View Jira History word by word
  6. // @author You
  7. // @match https://captiv8.atlassian.net/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=atlassian.net
  9. // @grant GM_addStyle
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function AddCompareButtons() {
  16. console.log('started')
  17. let parentNode = document.querySelector('div[data-testid="issue-history.ui.feed-container"]')
  18. Array.from(parentNode.childNodes).map(i => i.childNodes[1]).map(i => i.childNodes[0]).forEach((item, i) => {
  19. AddCompareButton(item, i)
  20. })
  21. }
  22.  
  23. function AddCompareButton(blockWithChanges, i) {
  24. //console.log([blockWithChanges, i])
  25. /*let compareButton = document.createElement ('div');
  26. let id = 'myButton' + i
  27. compareButton.innerHTML = '<button id="' + id + '" type="button">Compare</button>'
  28. compareButton.setAttribute ('id', 'myContainer');
  29. blockWithChanges.appendChild (compareButton);
  30. let ButtonClickAction = function() {*/
  31. let textNodes = blockWithChanges.parentNode.childNodes[1].childNodes
  32. let oldTextNode = textNodes[0]
  33. let oldText = oldTextNode.innerText
  34. let newTextNode = textNodes[2]
  35. let newText = newTextNode.innerText
  36. //oldTextNode.replaceChild(document.createTextNode(''), oldTextNode.firstChild)
  37. newTextNode.replaceChild(document.createTextNode(''), newTextNode.firstChild)
  38. //oldTextNode.innerText = ''
  39. newTextNode.innerText = ''
  40.  
  41. /*let textDiff = diff(oldText, newText)
  42. var fragment = document.createDocumentFragment();
  43. for (let i=0; i < textDiff.length; i++) {
  44.  
  45. if (textDiff[i].added && textDiff[i + 1] && textDiff[i + 1].removed) {
  46. let swap = textDiff[i];
  47. textDiff[i] = textDiff[i + 1];
  48. textDiff[i + 1] = swap;
  49. }
  50.  
  51. let node;
  52. if (textDiff[i].removed) {
  53. node = document.createElement('del');
  54. node.appendChild(document.createTextNode(textDiff[i].value));
  55. } else if (textDiff[i].added) {
  56. node = document.createElement('ins');
  57. node.appendChild(document.createTextNode(textDiff[i].value));
  58. } else {
  59. node = document.createTextNode(textDiff[i].value);
  60. }
  61. fragment.appendChild(node);
  62. }
  63.  
  64. oldTextNode.appendChild(fragment);*/
  65.  
  66.  
  67. let textDiff = diff(oldText, newText)
  68. var fragment2 = document.createDocumentFragment();
  69. for (let i=0; i < textDiff.length; i++) {
  70.  
  71. if (textDiff[i].added && textDiff[i + 1] && textDiff[i + 1].removed) {
  72. let swap = textDiff[i];
  73. textDiff[i] = textDiff[i + 1];
  74. textDiff[i + 1] = swap;
  75. }
  76.  
  77. let node;
  78. if (textDiff[i].removed) {
  79. node = document.createElement('del');
  80. node.appendChild(document.createTextNode(textDiff[i].value));
  81. } else if (textDiff[i].added) {
  82. node = document.createElement('ins');
  83. node.appendChild(document.createTextNode(textDiff[i].value));
  84. } else {
  85. node = document.createTextNode(textDiff[i].value);
  86. }
  87. fragment2.appendChild(node);
  88. }
  89.  
  90. newTextNode.appendChild(fragment2);
  91.  
  92. //}
  93. //document.getElementById (id).addEventListener ("click", ButtonClickAction, false);
  94. }
  95.  
  96. function AddStartButton() {
  97. let activityH2 = document.querySelector('h2[data-test-id="issue-activity-feed.heading"]')
  98. if (activityH2 === null) {
  99. console.log('not found')
  100. setTimeout(() => {
  101. AddStartButton()
  102. }, 1000)
  103. }
  104. console.log(activityH2)
  105. let parentNode = activityH2.parentNode
  106.  
  107. let startButton = document.createElement ('div');
  108. startButton.innerHTML = '<button id="StartCmp" type="button">Compare</button>'
  109. startButton.setAttribute ('id', 'myContainer');
  110. parentNode.appendChild (startButton);
  111. let ButtonClickAction = function() {
  112. console.log(blockWithCHanges)
  113. }
  114. document.getElementById ('StartCmp').addEventListener ("click", AddCompareButtons, false);
  115. }
  116.  
  117. window.addEventListener("load", (event) => {
  118. console.log('loaded')
  119. setTimeout(() => {
  120. AddStartButton()
  121. }, 1000)
  122. });
  123.  
  124. //--- Style our newly added elements using CSS.
  125. GM_addStyle ( `
  126. #myContainer {
  127. position: relative;
  128. top: 0%;
  129. right: 0%;
  130. font-size: 20px;
  131. z-index: 1100;
  132. }
  133. #myButton {
  134. cursor: pointer;
  135. }
  136. del {
  137. text-decoration: none;
  138. color: #b30000;
  139. background: #fadad7;
  140. }
  141. ins {
  142. background: #eaf2c2;
  143. color: #406619;
  144. text-decoration: none;
  145. }
  146. ` );
  147.  
  148. /*!
  149.  
  150. diff v2.0.1
  151.  
  152. Software License Agreement (BSD License)
  153.  
  154. Copyright (c) 2009-2015, Kevin Decker <kpdecker@gmail.com>
  155.  
  156. All rights reserved.
  157.  
  158. Redistribution and use of this software in source and binary forms, with or without modification,
  159. are permitted provided that the following conditions are met:
  160.  
  161. * Redistributions of source code must retain the above
  162. copyright notice, this list of conditions and the
  163. following disclaimer.
  164.  
  165. * Redistributions in binary form must reproduce the above
  166. copyright notice, this list of conditions and the
  167. following disclaimer in the documentation and/or other
  168. materials provided with the distribution.
  169.  
  170. * Neither the name of Kevin Decker nor the names of its
  171. contributors may be used to endorse or promote products
  172. derived from this software without specific prior
  173. written permission.
  174.  
  175. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  176. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  177. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  178. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  179. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  180. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  181. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  182. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  183. @license
  184. */
  185.  
  186. function diff(oldString, newString, callback) {
  187.  
  188. function done(value) {
  189. if (callback) {
  190. setTimeout(function () {
  191. callback(undefined, value);
  192. }, 0);
  193. return true;
  194. } else {
  195. return value;
  196. }
  197. }
  198.  
  199. // Allow subclasses to massage the input prior to running
  200. oldString = castInput(oldString);
  201. newString = castInput(newString);
  202.  
  203. // Handle the identity case (this is due to unrolling editLength == 0
  204. if (newString === oldString) {
  205. return done([{ value: newString }]);
  206. }
  207. if (!newString) {
  208. return done([{ value: oldString, removed: true }]);
  209. }
  210. if (!oldString) {
  211. return done([{ value: newString, added: true }]);
  212. }
  213.  
  214. newString = removeEmpty(tokenize(newString));
  215. oldString = removeEmpty(tokenize(oldString));
  216.  
  217. var newLen = newString.length,
  218. oldLen = oldString.length;
  219. var editLength = 1;
  220. var maxEditLength = newLen + oldLen;
  221. var bestPath = [{ newPos: -1, components: [] }];
  222.  
  223. // Seed editLength = 0, i.e. the content starts with the same values
  224. var oldPos = extractCommon(bestPath[0], newString, oldString, 0);
  225. if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
  226. // Identity per the equality and tokenizer
  227. return done([{ value: newString.join('') }]);
  228. }
  229.  
  230. // Main worker method. checks all permutations of a given edit length for acceptance.
  231. function execEditLength() {
  232. for (var diagonalPath = -1 * editLength; diagonalPath <= editLength; diagonalPath += 2) {
  233. var basePath = undefined;
  234. var addPath = bestPath[diagonalPath - 1],
  235. removePath = bestPath[diagonalPath + 1],
  236. _oldPos = (removePath ? removePath.newPos : 0) - diagonalPath;
  237. if (addPath) {
  238. // No one else is going to attempt to use this value, clear it
  239. bestPath[diagonalPath - 1] = undefined;
  240. }
  241.  
  242. var canAdd = addPath && addPath.newPos + 1 < newLen,
  243. canRemove = removePath && 0 <= _oldPos && _oldPos < oldLen;
  244. if (!canAdd && !canRemove) {
  245. // If this path is a terminal then prune
  246. bestPath[diagonalPath] = undefined;
  247. continue;
  248. }
  249.  
  250. // Select the diagonal that we want to branch from. We select the prior
  251. // path whose position in the new string is the farthest from the origin
  252. // and does not pass the bounds of the diff graph
  253. if (!canAdd || canRemove && addPath.newPos < removePath.newPos) {
  254. basePath = clonePath(removePath);
  255. pushComponent(basePath.components, undefined, true);
  256. } else {
  257. basePath = addPath; // No need to clone, we've pulled it from the list
  258. basePath.newPos++;
  259. pushComponent(basePath.components, true, undefined);
  260. }
  261.  
  262. _oldPos = extractCommon(basePath, newString, oldString, diagonalPath);
  263.  
  264. // If we have hit the end of both strings, then we are done
  265. if (basePath.newPos + 1 >= newLen && _oldPos + 1 >= oldLen) {
  266. let useLongestToken = true
  267. return done(buildValues(basePath.components, newString, oldString, useLongestToken));
  268. } else {
  269. // Otherwise track this path as a potential candidate and continue.
  270. bestPath[diagonalPath] = basePath;
  271. }
  272. }
  273.  
  274. editLength++;
  275. }
  276.  
  277. // Performs the length of edit iteration. Is a bit fugly as this has to support the
  278. // sync and async mode which is never fun. Loops over execEditLength until a value
  279. // is produced.
  280. if (callback) {
  281. (function exec() {
  282. setTimeout(function () {
  283. // This should not happen, but we want to be safe.
  284. /* istanbul ignore next */
  285. if (editLength > maxEditLength) {
  286. return callback();
  287. }
  288.  
  289. if (!execEditLength()) {
  290. exec();
  291. }
  292. }, 0);
  293. })();
  294. } else {
  295. while (editLength <= maxEditLength) {
  296. var ret = execEditLength();
  297. if (ret) {
  298. return ret;
  299. }
  300. }
  301. }
  302. }
  303.  
  304. function pushComponent(components, added, removed) {
  305. var last = components[components.length - 1];
  306. if (last && last.added === added && last.removed === removed) {
  307. // We need to clone here as the component clone operation is just
  308. // as shallow array clone
  309. components[components.length - 1] = { count: last.count + 1, added: added, removed: removed };
  310. } else {
  311. components.push({ count: 1, added: added, removed: removed });
  312. }
  313. }
  314. function extractCommon(basePath, newString, oldString, diagonalPath) {
  315. var newLen = newString.length,
  316. oldLen = oldString.length,
  317. newPos = basePath.newPos,
  318. oldPos = newPos - diagonalPath,
  319. commonCount = 0;
  320. while (newPos + 1 < newLen && oldPos + 1 < oldLen && equals(newString[newPos + 1], oldString[oldPos + 1])) {
  321. newPos++;
  322. oldPos++;
  323. commonCount++;
  324. }
  325.  
  326. if (commonCount) {
  327. basePath.components.push({ count: commonCount });
  328. }
  329.  
  330. basePath.newPos = newPos;
  331. return oldPos;
  332. }
  333.  
  334. function equals(left, right) {
  335. var reWhitespace = /\S/;
  336. let ignoreWhitespace = false
  337. return left === right || ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right);
  338. }
  339. function removeEmpty(array) {
  340. var ret = [];
  341. for (var i = 0; i < array.length; i++) {
  342. if (array[i]) {
  343. ret.push(array[i]);
  344. }
  345. }
  346. return ret;
  347. }
  348. function castInput(value) {
  349. return value;
  350. }
  351. function tokenize(value) {
  352. var extendedWordChars = /^[A-Za-z\xC0-\u02C6\u02C8-\u02D7\u02DE-\u02FF\u1E00-\u1EFF]+$/;
  353. var tokens = value.split(/(\s+|\b)/);
  354.  
  355. // Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set.
  356. for (var i = 0; i < tokens.length - 1; i++) {
  357. // If we have an empty string in the next field and we have only word chars before and after, merge
  358. if (!tokens[i + 1] && tokens[i + 2] && extendedWordChars.test(tokens[i]) && extendedWordChars.test(tokens[i + 2])) {
  359. tokens[i] += tokens[i + 2];
  360. tokens.splice(i + 1, 2);
  361. i--;
  362. }
  363. }
  364. return tokens;
  365. }
  366.  
  367. function buildValues(components, newString, oldString, useLongestToken) {
  368. var componentPos = 0,
  369. componentLen = components.length,
  370. newPos = 0,
  371. oldPos = 0;
  372.  
  373. for (; componentPos < componentLen; componentPos++) {
  374. var component = components[componentPos];
  375. if (!component.removed) {
  376. if (!component.added && useLongestToken) {
  377. var value = newString.slice(newPos, newPos + component.count);
  378. value = map(value, function (value, i) {
  379. var oldValue = oldString[oldPos + i];
  380. return oldValue.length > value.length ? oldValue : value;
  381. });
  382.  
  383. component.value = value.join('');
  384. } else {
  385. component.value = newString.slice(newPos, newPos + component.count).join('');
  386. }
  387. newPos += component.count;
  388.  
  389. // Common case
  390. if (!component.added) {
  391. oldPos += component.count;
  392. }
  393. } else {
  394. component.value = oldString.slice(oldPos, oldPos + component.count).join('');
  395. oldPos += component.count;
  396.  
  397. // Reverse add and remove so removes are output first to match common convention
  398. // The diffing algorithm is tied to add then remove output and this is the simplest
  399. // route to get the desired output with minimal overhead.
  400. if (componentPos && components[componentPos - 1].added) {
  401. var tmp = components[componentPos - 1];
  402. components[componentPos - 1] = components[componentPos];
  403. components[componentPos] = tmp;
  404. }
  405. }
  406. }
  407.  
  408. return components;
  409. }
  410. function clonePath(path) {
  411. return { newPos: path.newPos, components: path.components.slice(0) };
  412. }
  413. function map(arr, mapper, that) {
  414. if (Array.prototype.map) {
  415. return Array.prototype.map.call(arr, mapper, that);
  416. }
  417.  
  418. var other = new Array(arr.length);
  419.  
  420. for (var i = 0, n = arr.length; i < n; i++) {
  421. other[i] = mapper.call(that, arr[i], i, arr);
  422. }
  423. return other;
  424. }
  425. })();

QingJ © 2025

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