Amboss Copier

Copy article informations from Miamed Amboss.

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

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