AO3: Tracking

Track any filterable listing.

目前为 2015-11-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name AO3: Tracking
  3. // @description Track any filterable listing.
  4. // @namespace https://greasyfork.org/en/scripts/8382-ao3-tracking
  5. // @author Min
  6. // @version 1.3.1
  7. // @grant none
  8. // @include http://archiveofourown.org/*
  9. // @include https://archiveofourown.org/*
  10. // ==/UserScript==
  11.  
  12.  
  13. (function($) {
  14. if (typeof(Storage) !== 'undefined') {
  15. var tracked_list = '';
  16. var tracked_array = [];
  17. loadList();
  18. addCss();
  19. addTrackedMenu();
  20. var main = $('#main');
  21. // if it's a listing of works or bookmarks
  22. if (main.hasClass('works-index') || main.hasClass('bookmarks-index')) {
  23. var is_tracked = false;
  24. var is_first_page = true;
  25. // get page url
  26. var location_url = location.href;
  27. // check if page is already tracked
  28. var array_index = tracked_array.indexOf(location_url);
  29. if (array_index > -1) {
  30. is_tracked = true;
  31. checkOpen();
  32. }
  33. // make sure it's the first page of the listing
  34. var current_page = main.find('ol.pagination:first span.current');
  35. if (current_page.length && current_page.text() !== '1') {
  36. is_first_page = false;
  37. }
  38. if (is_first_page) { addTrackButton(); }
  39. }
  40. // add "Mark all as vieved" button when all listing get checked
  41. $(document).ajaxStop(function() {
  42. if ($('#tracked-box').length && !$('#button-check').length) {
  43. $('#button-mark').css('visibility', 'visible');
  44. }
  45. });
  46. }
  47. // load the saved list
  48. function loadList() {
  49. tracked_list = localStorage.getItem('ao3tracking_list');
  50. if (!tracked_list) { tracked_list = ''; }
  51. // make an array of the list
  52. if (tracked_list.length) {
  53. tracked_array = tracked_list.split(',,,');
  54. }
  55. }
  56. // add current page to tracked listings
  57. function addToTracked() {
  58. var added = false;
  59. loadList();
  60. // if there's less than 25 tracked
  61. if (tracked_array.length < 75) {
  62. // ask for name
  63. var heading = main.find('h2.heading:first');
  64. var heading_link = heading.find('a');
  65. if (heading_link.length) {
  66. var suggest = heading_link.text();
  67. }
  68. else {
  69. var suggest = heading.text().replace(/\n/g, '').replace(/^\s+/, '').replace(/(.+of )?\d+ /, '');
  70. }
  71. var listing_name = prompt('Name for the tracked listing:', suggest);
  72. if (listing_name !== '' && listing_name !== null) {
  73. // remove characters we don't want in the names
  74. listing_name = listing_name.replace(/,,,/g, ' ');
  75. var listing_count = heading.text().replace(/\n/g, '').replace(/^\s+/, '').replace(/.*\d+ - \d+ of /, '').replace(/(\d+)(.+)/, '$1');
  76. // add name, url, count
  77. tracked_array.push(listing_name, location_url, listing_count);
  78. tracked_list = tracked_array.join(',,,');
  79. // save the updated list
  80. localStorage.setItem('ao3tracking_list', tracked_list);
  81. added = true;
  82. }
  83. }
  84. else {
  85. alert("You're already tracking 25 listings. Remove some first.");
  86. }
  87. return added;
  88. }
  89. // remove a given url from tracked listings
  90. function removeFromTracked(url) {
  91. var removed = false;
  92. loadList();
  93. var index = tracked_array.indexOf(url);
  94. // if the url is on the saved list
  95. if (index > -1) {
  96. // ask for confirmation
  97. var confirmed = confirm('Sure you want to remove "' + tracked_array[index-1] + '"?');
  98. if (confirmed) {
  99. // remove name, url, count
  100. tracked_array.splice(index-1, 3);
  101. tracked_list = tracked_array.join(',,,');
  102. // save the updated list
  103. localStorage.setItem('ao3tracking_list', tracked_list);
  104. removed = true;
  105. }
  106. }
  107. return removed;
  108. }
  109. // check open page for new works
  110. function checkOpen() {
  111. var heading = main.find('h2.heading:first');
  112. // get a count of new works
  113. var current_count = parseInt(heading.text().replace(/\n/g, '').replace(/^\s+/, '').replace(/.*\d+ - \d+ of /, '').replace(/(\d+)(.+)/, '$1'));
  114. var saved_count = parseInt(tracked_array[array_index+1]);
  115. var new_count = current_count - saved_count;
  116. if (new_count !== 0) {
  117. heading.append(' <span id="new-works">(' + new_count + ' new)</span> <a id="mark-viewed">[mark viewed]</a>');
  118. $('#mark-viewed').click(function() {
  119. loadList();
  120. var array_index = tracked_array.indexOf(location_url);
  121. if (array_index > -1) {
  122. // update the count
  123. tracked_array[array_index+1] = current_count;
  124. tracked_list = tracked_array.join(',,,');
  125. // save the updated list
  126. localStorage.setItem('ao3tracking_list', tracked_list);
  127. }
  128. $('#new-works').detach();
  129. $(this).detach();
  130. });
  131. }
  132. }
  133. // check the tracked listings for new works
  134. function checkForNew() {
  135. // check if it's more than 8 hours since last check
  136. var last_check = localStorage.getItem('ao3tracking_lastcheck');
  137. if (!last_check) { var last_check = 0; }
  138. else { last_check = parseInt(last_check); }
  139. var now = new Date();
  140. now = now.getTime();
  141. var wait = 28800000 - (now - last_check);
  142. if (wait < 0) {
  143. localStorage.setItem('ao3tracking_lastcheck', now);
  144. // for each tracked listing
  145. $('#tracked-box li.tracked-listing').each(function() {
  146. var tracked_url = $(this).find('a').attr('href');
  147. var listing_id = $(this).attr('id');
  148. tracked_url += ' #main h2.heading:first';
  149. // load heading of the tracked page
  150. $(this).find('span.tracked-current').load(tracked_url, function() {
  151. var listing = $('#' + listing_id);
  152. // get a count of new works
  153. var current_count = parseInt(listing.find('span.tracked-current').text().replace(/\n/g, '').replace(/^\s+/, '').replace(/.*\d+ - \d+ of /, '').replace(/(\d+)(.+)/, '$1'));
  154. listing.find('span.tracked-current').html(current_count);
  155. var saved_count = parseInt(listing.find('span.tracked-saved').text());
  156. var new_count = current_count - saved_count;
  157. listing.find('span.tracked-new').text('(' + new_count + ' new)');
  158. if (new_count !== 0) {
  159. listing.find('span.tracked-new').addClass('new-stuff');
  160. listing.parent().prepend(listing);
  161. }
  162. else {
  163. listing.find('span.tracked-new').addClass('no-new-stuff');
  164. }
  165. });
  166. });
  167. }
  168. else {
  169. var hours = Math.floor(wait/3600000);
  170. var minutes = Math.ceil((wait%3600000)/60000);
  171. var warning = $('<p style="color: #990000;"></p>');
  172. if (hours > 0) {
  173. warning.html('<strong>Please be kind to the AO3 servers!</strong> Wait ' + hours + ' hour(s) and ' + minutes + ' minute(s) more before another check.');
  174. }
  175. else {
  176. warning.html('<strong>Please be kind to the AO3 servers!</strong> Wait ' + minutes + ' more minute(s) before another check.');
  177. }
  178. $('#tracked-box p.actions').after(warning);
  179. }
  180. }
  181. // add the 'Track This' button
  182. function addTrackButton() {
  183. var work_filters = $('form.filters dd.submit.actions:first');
  184. var track_this_button = $('<input type="button" value="Track This" class="track-this"></input>');
  185. track_this_button.click(function() {
  186. var added = addToTracked();
  187. if (added) {
  188. track_this_button.detach();
  189. work_filters.prepend(untrack_this_button);
  190. }
  191. });
  192. var untrack_this_button = $('<input type="button" value="Untrack This" class="track-this"></input>');
  193. untrack_this_button.click(function() {
  194. var removed = removeFromTracked(location_url);
  195. if (removed) {
  196. untrack_this_button.detach();
  197. work_filters.prepend(track_this_button);
  198. }
  199. });
  200. // if the page is already tracked
  201. if (is_tracked) {
  202. work_filters.prepend(untrack_this_button);
  203. }
  204. // if it's not tracked
  205. else {
  206. work_filters.prepend(track_this_button);
  207. }
  208. }
  209. // rearrange things on the list
  210. function editList() {
  211. var box_list = $('#box-list');
  212. box_list.find('li.tracked-listing').each(function() {
  213. $(this).prepend('<span class="up-arrow">&uarr;</span> <span class="down-arrow">&darr;</span> <span class="cross">&cross;</span> ');
  214. });
  215. box_list.on('click', 'span.up-arrow', function() {
  216. $(this).parent().prev().before($(this).parent());
  217. });
  218. box_list.on('click', 'span.down-arrow', function() {
  219. $(this).parent().next().after($(this).parent());
  220. });
  221. box_list.on('click', 'span.cross', function() {
  222. $(this).parent().detach();
  223. });
  224. }
  225. // save list after edits
  226. function saveList() {
  227. tracked_array = [];
  228. // get name, url, count for all listings
  229. $('#tracked-box li.tracked-listing').each(function() {
  230. var name = $(this).find('a').text();
  231. var url = $(this).find('a').attr('href');
  232. var count = $(this).find('span.tracked-saved').text();
  233. tracked_array.push(name, url, count);
  234. });
  235. // update and save the new list
  236. tracked_list = tracked_array.join(',,,');
  237. localStorage.setItem('ao3tracking_list', tracked_list);
  238. // reload the box
  239. $('#tracked-box').detach();
  240. $('#tracked-bg').detach();
  241. showBox();
  242. }
  243. // update the listings counts
  244. function markAllViewed() {
  245. loadList();
  246. // get the current count for all listings
  247. $('#tracked-box li.tracked-listing').each(function() {
  248. var url = $(this).find('a').attr('href');
  249. var current_count = $(this).find('span.tracked-current').text();
  250. var index = tracked_array.indexOf(url);
  251. if (index > -1) {
  252. tracked_array[index+1] = current_count;
  253. }
  254. });
  255. // update and save the new list
  256. tracked_list = tracked_array.join(',,,');
  257. localStorage.setItem('ao3tracking_list', tracked_list);
  258. // reload the box
  259. $('#tracked-box').detach();
  260. $('#tracked-bg').detach();
  261. showBox();
  262. }
  263. // show the box with tracked listings
  264. function showBox() {
  265. var tracked_bg = $('<div id="tracked-bg"></div>');
  266. var tracked_box = $('<div id="tracked-box"></div>');
  267. var box_buttons = $('<p class="actions"></p>');
  268. var box_button_check = $('<input type="button" id="button-check" value="Check for new"></input>');
  269. box_button_check.click(function() {
  270. box_button_edit.after(box_button_mark);
  271. checkForNew();
  272. box_button_edit.detach();
  273. box_button_check.detach();
  274. });
  275. var box_button_edit = $('<input type="button" id="button-edit" value="Edit list"></input>');
  276. box_button_edit.click(function() {
  277. editList();
  278. box_button_edit.after(box_button_save, box_button_cancel);
  279. box_button_check.detach();
  280. box_button_edit.detach();
  281. });
  282. var box_button_save = $('<input type="button" id="button-save" value="Save list"></input>');
  283. box_button_save.click(function() { saveList(); });
  284. var box_button_cancel = $('<input type="button" id="button-cancel" value="Cancel edits"></input>');
  285. box_button_cancel.click(function() {
  286. tracked_box.detach();
  287. tracked_bg.detach();
  288. showBox();
  289. });
  290. var box_button_mark = $('<input type="button" id="button-mark" style="visibility: hidden;" value="Mark all as viewed"></input>');
  291. box_button_mark.click(function() { markAllViewed(); });
  292. var box_button_close = $('<input type="button" id="button-close" value="Close"></input>');
  293. box_button_close.click(function() {
  294. tracked_box.detach();
  295. tracked_bg.detach();
  296. });
  297. var box_header = $('<h3></h3>').text('Tracked listings [' + tracked_array.length/3 + '/25]:');
  298. var box_list = $('<ul id="box-list"></ul>');
  299. tracked_box.append(box_buttons, box_header, box_list);
  300. // if there are saved listings
  301. if (tracked_array.length > 2) {
  302. for (var i = 0; i < tracked_array.length; i += 3) {
  303. var listing = $('<li id="tracked-listing-' + i/3 + '" class="tracked-listing"></li>').html('<a href="' + tracked_array[i+1] + '">' + tracked_array[i] + '</a> <span class="tracked-new"></span> <span class="tracked-saved">' + tracked_array[i+2] + '</span> <span class="tracked-current"></span>');
  304. box_list.append(listing);
  305. }
  306. }
  307. else {
  308. var no_listings = $('<li style="opacity: 0.5; font-style: oblique;"></li>').html("you're not tracking anything yet!");
  309. box_list.append(no_listings);
  310. box_button_check.css('visibility', 'hidden');
  311. box_button_edit.css('visibility', 'hidden');
  312. }
  313. box_buttons.append(box_button_check, box_button_edit, box_button_close);
  314. $('body').append(tracked_bg, tracked_box);
  315. }
  316. // attach the menu
  317. function addTrackedMenu() {
  318. // get the header menu
  319. var header_menu = $('ul.primary.navigation.actions');
  320. // create and insert menu button
  321. var tracked_button = $('<input class="button" type="button" value="Tracked"></input>');
  322. header_menu.find('#search').prepend(tracked_button);
  323. tracked_button.click(function() {
  324. if ($('#tracked-box').length == 0) {
  325. loadList();
  326. showBox();
  327. }
  328. });
  329. }
  330. // add css rules to page head
  331. function addCss() {
  332. var style = $('<style type="text/css"></style>').appendTo($('head'));
  333. var css = '#tracked-box {position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px; width: 60%; height: 80%; max-width: 800px; margin: auto; overflow-y: auto; border: 10px solid #eee; box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.2); padding: 0 20px; background-color: #ffffff; z-index: 999;}\
  334. #tracked-bg {position: fixed; width: 100%; height: 100%; background-color: #000000; opacity: 0.7; z-index: 998;}\
  335. input[type="button"] {height: auto;}\
  336. input.track-this {margin-bottom: 10px;}\
  337. #tracked-box p.actions {float: none; text-align: left;}\
  338. #button-save {font-weight: bold;}\
  339. #button-close {float: right;}\
  340. #tracked-box li span.tracked-new.new-stuff {font-weight: bold;}\
  341. #tracked-box li span.tracked-new.no-new-stuff {opacity: 0.5;}\
  342. #tracked-box li span.tracked-current, #tracked-box li span.tracked-saved {display: none;}\
  343. #new-works {font-weight: bold;}';
  344. style.append(css);
  345. }
  346. })(jQuery);