Filmtipset mark nicks.

Mark your own and friends names with a little square.

当前为 2014-10-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Filmtipset mark nicks.
  3. // @version 0.1.1
  4. // @description Mark your own and friends names with a little square.
  5. // @namespace https://github.com/Row/filmtipset-userscripts
  6. // @include http://nyheter24.se/filmtipset/*
  7. // ==/UserScript==
  8.  
  9. function isChrome()
  10. {
  11. return navigator.userAgent.toLowerCase().indexOf('chrome') > -1 && GM_info == undefined;
  12. }
  13.  
  14. /**
  15. * Peform a get xmlhttpRequest with a callback function
  16. */
  17. function get(url, callBack)
  18. {
  19. GM_xmlhttpRequest({
  20. method: "GET",
  21. url: url,
  22. onload: function(xhr) { callBack(xhr.responseText); }
  23. });
  24. }
  25.  
  26. /**
  27. * Add css style to the document.
  28. */
  29. function addStyle(css)
  30. {
  31. var head, style;
  32. head = document.getElementsByTagName('head')[0];
  33. if (!head) { return; }
  34. style = document.createElement('style');
  35. style.type = 'text/css';
  36. try {
  37. style.innerHTML = css;
  38. } catch(err) {
  39. style.innerText = css;
  40. }
  41. head.appendChild(style);
  42. }
  43.  
  44. function FriendHandler()
  45. {
  46. var friends;
  47. try {
  48. friends = JSON.parse(GM_getValue("filmtipsetFriends"))
  49. } catch(err) {
  50. /**
  51. If using Google Chrome add a list of people you would like to
  52. mark as friend in the forum, i.e
  53. friends = ['Morgan','Powha','Skeletor'];
  54. **/
  55. friends = [];
  56. }
  57.  
  58. var me = document.querySelectorAll(
  59. 'div.menuitemtitle'
  60. )[7].innerHTML.replace(/<.+>/,'');
  61.  
  62. var updateFriends = function (friendPageContent)
  63. {
  64. var offset = 21600000;
  65. var nextUpdate = new Date().getTime() + offset;
  66. friends = [];
  67. friendPageContent.replace(
  68. /<a class="member" .*?>(.*?)(,|)&nbsp;<\/a>/gm,
  69. function(m, n) {friends.push(n)}
  70. )
  71. GM_setValue("filmtipsetFriends", JSON.stringify(friends));
  72. GM_setValue("filmtipsetNextFriendUpdate", nextUpdate.toString());
  73. }
  74.  
  75. this.checkUpdate = function()
  76. {
  77. var nextUpdate = + GM_getValue("filmtipsetNextFriendUpdate");
  78. var url = 'http://nyheter24.se/filmtipset/yourpage.cgi?page=friends_online';
  79. var now = new Date().getTime();
  80. if(nextUpdate < now || !nextUpdate) {
  81. get(url, updateFriends);
  82. }
  83. }
  84.  
  85. this.getFriends = function()
  86. {
  87. return friends;
  88. }
  89.  
  90. this.isFriend = function(name)
  91. {
  92. return friends.indexOf(name) > -1;
  93. }
  94.  
  95. this.isMe = function(name)
  96. {
  97. return me == name;
  98. }
  99.  
  100. if(!isChrome())
  101. this.checkUpdate();
  102.  
  103. }
  104.  
  105. addStyle(
  106. 'a.friend, a.me {' +
  107. ' padding-left: 10px;' +
  108. ' background: url("http://filmtipset.powha.net/ext/friend.png") no-repeat 0 50%;' +
  109. '}' +
  110. 'a.me {' +
  111. ' background: url("http://filmtipset.powha.net/ext/me.png") no-repeat 0 50%;' +
  112. '}'
  113. );
  114.  
  115. var friends = new FriendHandler();
  116. var persons = document.querySelectorAll('.member');
  117.  
  118. for(var i in persons) {
  119. if(friends.isFriend(persons[i].innerHTML))
  120. persons[i].className += ' friend';
  121.  
  122. if(friends.isMe(persons[i].innerHTML))
  123. persons[i].className += ' me';
  124. }