Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        WaniKani Time-Spent
  3. // @namespace   rfindley
  4. // @description Keep track of your time spend on WaniKani.
  5. // @version     1.0.0
  6. // @include     https://www.wanikani.com/*
  7. // @copyright   2015+, Robin Findley
  8. // @license     MIT; http://opensource.org/licenses/MIT
  9. // @run-at      document-end
  10. // @grant       none
  11. // ==/UserScript==
  12.  
  13. wktimespent = {};
  14.  
  15. (function(gobj) {
  16.     var start_time, stats, active={};
  17.  
  18.     var categories = {
  19.         all: ['*'],
  20.         dashboard: ['/dashboard'],
  21.         forums: ['/chat/*'],
  22.         forum_api: ['/chat/api-and-third-party-apps*'],
  23.         forum_campfire: ['/chat/campfire*'],
  24.         forum_kanji: ['/chat/kanji-and-japanese*'],
  25.         forum_wanikani: ['/chat/wanikani*'],
  26.         lessons: ['/lesson*'],
  27.         reviews: ['/review*'],
  28.         study: ['/level*','/radical*','/kanji*','/vocabulary*']
  29.     };
  30.  
  31.     function save_stats() {
  32.         var stop_time = Math.round(new Date().getTime()/1000);
  33.        
  34.         $.each(active, function(category,value){
  35.             var catname = 'timespent_'+category;
  36.             var timespent = Number(localStorage.getItem(catname) || 0);
  37.             timespent += (stop_time - start_time);
  38.             localStorage.setItem(catname, timespent);
  39.         });
  40.     }
  41.    
  42.     function main() {
  43.         start_time = Math.round(new Date().getTime()/1000);
  44.  
  45.         // Figure out what timers we need to increment.
  46.         var page = window.location.pathname;
  47.         $.each(categories, function(category, urls){
  48.             $.each(urls, function(idx, url){
  49.                 var criteria = '^'+url.replace('\*','.*')+'$';
  50.                 if (page.match(criteria) != null)
  51.                     active[category] = 1;
  52.             });
  53.         });
  54.  
  55.         $(window).unload(save_stats);
  56.     }
  57.  
  58.     // Run startup() after window.onload event.
  59.     if (document.readyState === 'complete')
  60.         main();
  61.     else
  62.         window.addEventListener("load", main, false);
  63.  
  64. }(wktimespent));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement