Amboss Copier

Copy article informations from Miamed Amboss.

当前为 2016-09-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Amboss Copier
  3. // @namespace http://oix.cc/gm
  4. // @description Copy article informations from Miamed Amboss.
  5. // @author Bin Zhang
  6. // @icon https://amboss.miamed.de/favicon-192x192.png
  7. // @homepageURL http://oix.cc/amboss
  8. // @version 0.0.5
  9. // @match https://amboss.miamed.de/library
  10. // @include /^https?://amboss-miamed-de\.ezproxy\..*/library$/
  11. // @grant GM_setClipboard
  12. // @run-at document-idle
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. var loadingWatcher;
  17. var currentContent;
  18.  
  19. if (typeof angular !== 'undefined' || typeof angular.element('#LibraryContent').scope() !== 'undefined') {
  20. setWatcher();
  21. window.addEventListener("hashchange", setWatcher);
  22. document.addEventListener('keydown', function (e) {
  23. if ('card' === currentContent) {
  24. //if ((e.key == 'c' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) || (e.key == 'F9' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey)) {
  25. if ((e.key == 'c' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey)) {
  26. // pressed ctrl+alt+c or //F9
  27. GM_setClipboard(getCardHtml(), 'html');
  28. } else if (e.key == 'y' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  29. // pressed ctrl+alt+y
  30. GM_setClipboard(getCardHtml(true), 'html');
  31. } else if (e.key == 'w' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  32. // pressed ctrl+alt+w
  33. $('div.Tab.active .js-prev-day').click();
  34. } else if (e.key == 's' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  35. // pressed ctrl+alt+s
  36. $('div.Tab.active .js-next-day').click();
  37. } else if (e.key == 'a' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  38. // pressed ctrl+alt+a
  39. $('div.Tab.active .js-prev-lc').click();
  40. } else if (e.key == 'd' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  41. // pressed ctrl+alt+d
  42. $('div.Tab.active .js-next-lc').click();
  43. }
  44. } else if ('list' === currentContent) {
  45. if (e.key == 'c' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  46. // pressed ctrl+alt+c
  47. GM_setClipboard(getListHtml(), 'html');
  48. } else if (e.key == 't' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  49. // pressed ctrl+alt+t
  50. GM_setClipboard(getListHtml(true), 'html');
  51. } else if (e.key == 'w' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  52. // pressed ctrl+alt+w
  53. $('div.Tab.active .js-prev-day').click();
  54. } else if (e.key == 's' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  55. // pressed ctrl+alt+s
  56. $('div.Tab.active .js-next-day').click();
  57. } else if (e.key == 'a' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  58. // pressed ctrl+alt+a
  59. $('div.Tab.active .js-prev-lc').click();
  60. } else if (e.key == 'd' && !e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
  61. // pressed ctrl+alt+d
  62. $('div.Tab.active .js-next-lc').click();
  63. }
  64. }
  65. }, false);
  66. }
  67.  
  68. function setWatcher() {
  69. currentContent = null;
  70.  
  71. var i;
  72. if (!i) i = 50;
  73. if (null !== getParameterByName('xid', '?' + window.location.hash.substring(1))) {
  74. // only cards set $root.loading, poll loading
  75. loadingWatcher = setInterval(checkLoading, i);
  76. } else {
  77. i = 2000;
  78. loadingWatcher = setInterval(function () {
  79. clearInterval(loadingWatcher);
  80. loaded();
  81. }, i);
  82. }
  83. //console.log('i=' + i);
  84. }
  85.  
  86. function checkLoading() {
  87. //console.log('watch');
  88. //console.log(angular.element('#LibraryContent').scope().$root.loading);
  89.  
  90. if (angular.element('#LibraryContent').scope().$root.loading !== true) {
  91. //console.log(typeof angular.element('#LibraryContent').scope().$root.loading);
  92. clearInterval(loadingWatcher);
  93. loaded();
  94. }
  95. }
  96.  
  97. function loaded() {
  98. //console.log('loaded');
  99. //console.log($('#LibraryContent').html());
  100.  
  101. // TODO: Change title of kreuzen
  102. // https://amboss.miamed.de/study2/index#/aB123Cdef/45
  103. // $('<div/>').html($('[tooltip-classname="lg"]').attr('tooltip-content')).text().replace(/^(Sitzung: )/,'');
  104. // "Examens-Kreuzplan (H. 2016), Tag 12 (Frage 34 von 56)"
  105.  
  106. if (null !== getParameterByName('xid', '?' + window.location.hash.substring(1)) && $('#LibraryContent').find('article.LearningCard:visible').length > 0) {
  107. //console.log('card');
  108. currentContent = 'card';
  109. }
  110. if ($('#LibraryContent').find('#LibraryList:visible').length > 0) {
  111. //console.log('list');
  112. currentContent = 'list';
  113. document.title = getListTitle(); // change browser window title if proper
  114. }
  115. }
  116.  
  117. function getCardHtml(full) {
  118. var html = '';
  119. html += '<h1><a href="' + window.location + '">' + $.trim($('article.LearningCard h1:first').clone().children().remove().end().text()) +
  120. ($('article.LearningCard h1:first').attr('tooltip-content') ? (' (' + $('article.LearningCard h1:first').attr('tooltip-content').replace(/^(Synonyme:<br> )/, '') + ')') : '') +
  121. '</a></h1>';
  122. var tempDiv;
  123. if (full) {
  124. tempDiv = $('<div>').append($('article.LearningCard>section').clone().find('section, .unwichtig, .veraltet').remove().end().find('li:emptyorspace').remove().end()); // remove meta infos, veraltet and unwichtig
  125. //.find('ul:visible:empty, ol:visible:empty').remove().end()
  126. //.find('ul:empty, ol:empty').remove().end()
  127. } else {
  128. tempDiv = $('<div>').append($('article.LearningCard>section:first p').clone()); // only p extracted, first section without p probably not abstracts
  129. }
  130. tempDiv.find('a').each(function (i, anchor) {
  131. anchor.href = $(anchor).prop('href'); // all urls to abs
  132. })
  133. html += tempDiv.html();
  134. tempDiv.remove();
  135. tempDiv = null;
  136. return html;
  137. }
  138.  
  139. function getListHtml(tableFormat) {
  140. var html = '';
  141. var listTitle = getListTitle();
  142. document.title = listTitle;
  143.  
  144. if (!tableFormat) {
  145. html += '<h2><a href="' + window.location + '">' + listTitle + '</a></h2>';
  146. var tempDiv = $('<div>')
  147. .append($('table#LibraryList').clone().find('td:not(:nth-child(2), :nth-child(3), :nth-child(8), :nth-child(9))').remove().end().find('thead').remove().end())
  148. .append($('<div>').append($('table#LibraryList').next('div:has(a.buttonized)').find('a.buttonized').clone())); // Kreuzen
  149. //tempDiv.hide();
  150. //$('body').append(tempDiv);
  151. tempDiv.find('a').each(function (i, anchor) {
  152. anchor.href = $(anchor).prop('href'); // all urls to abs
  153. })
  154. html += tempDiv.html();
  155. tempDiv.remove();
  156. tempDiv = null;
  157. } else { // output table format for excel
  158. var tempDiv = $('<div>')
  159. .append($('table#LibraryList').clone().find('thead, table, hr').remove().end().find('.inactive, .unset').remove().end().find('tr').prepend($('<td><a href="' + window.location + '">' + listTitle + '</a></td>')).end());
  160. //tempDiv.hide();
  161. //$('body').append(tempDiv);
  162. tempDiv.find('a').each(function (i, anchor) {
  163. anchor.href = $(anchor).prop('href'); // all urls to abs
  164. })
  165. html += tempDiv.html();
  166. tempDiv.remove();
  167. tempDiv = null;
  168. }
  169. return html;
  170. }
  171.  
  172. function getListTitle() {
  173. var listTitle = $.trim($('#LibraryContent').find('#Title').text());
  174. var examDay = $.trim($('div.Tab.active li.result-selected').text());
  175. var checkStr = examDay ? $.trim(examDay.substring(0, 6)) : ''; // 'Tag 1 ' or 'Tag 99'
  176. if (examDay && checkStr.length > 2 && listTitle.substr(-checkStr.length) === checkStr) {
  177. listTitle = examDay;
  178. }
  179. return listTitle;
  180. }
  181.  
  182. function getParameterByName(name, url) {
  183. if (!url) url = window.location.href;
  184. name = name.replace(/[\[\]]/g, "\\$&");
  185. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  186. results = regex.exec(url);
  187. if (!results) return null;
  188. if (!results[2]) return '';
  189. return decodeURIComponent(results[2].replace(/\+/g, " "));
  190. }
  191.  
  192. $.expr[':'].notext = function detectNoText(x) {
  193. return x.innerHTML && x.innerHTML.replace(/(<!--.*(?!-->))|\s+/g, '').length === 0
  194. }
  195.  
  196. $.expr[':'].emptyorspace = function detectNoText(x) {
  197. return x.innerHTML.replace(/(<!--.*(?!-->))|\s+/g, '').length === 0
  198. }
  199.  
  200. }());