TiebaOldUserCard

贴吧旧名片

  1. // ==UserScript==
  2. // @name TiebaOldUserCard
  3. // @author Crab
  4. // @namespace TiebaOldUserCard@tieba.com
  5. // @description 贴吧旧名片
  6. // @include http://tieba.baidu.com/p/*
  7. // @include http://tieba.baidu.com/f?*
  8. // @include http://tieba.baidu.com/f/*
  9. // @include http://www.baidu.com/forbiddenip/forbidden.html?xhr=TiebaOldUserCard
  10. // @version 2015.05.27.0.2
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function(){
  15. 'use strict';
  16.  
  17. if(location.host === 'www.baidu.com'){
  18. addEventListener('message', function(e){
  19. if(e.data.name !== 'tiebaOldUserCard' || !e.data.user) return;
  20. var xhr = new XMLHttpRequest();
  21. xhr.open('GET', 'http://www.baidu.com/p/'+ e.data.user +'?from=tieba', true);
  22. xhr.send(null);
  23. xhr.onload = function(){
  24. var r = xhr.responseText.match(/public_home\\\/user\\\/(\d+)\?domain/);
  25. e.source.postMessage({name: 'tiebaOldUserCard', uk: r ? r[1] : 'none', msgId: e.data.msgId}, e.origin);
  26. }
  27. }, false);
  28.  
  29. return;
  30. }
  31.  
  32. var cardList = {},
  33. hideCardTimeout = null,
  34. showCardTimeout = null,
  35. ucp = document.createElement('div'),
  36. crossFrame = ucp.appendChild(document.createElement('iframe')),
  37. crossFrameUrl = 'http://www.baidu.com/forbiddenip/forbidden.html?xhr=TiebaOldUserCard';
  38.  
  39. document.head.appendChild(document.createElement('style')).textContent
  40. = '#users_card_panel{'
  41. + 'display:none; border:1px solid #666; width:330px; background:#fff;'
  42. + 'position:absolute; z-index:100000000; overflow:hidden;}'
  43. + '#users_card_panel>iframe{'
  44. + 'border:none; display:none;}';
  45.  
  46. ucp.id = 'users_card_panel';
  47. crossFrame.src = crossFrameUrl;
  48. document.body.appendChild(ucp);
  49.  
  50. document.addEventListener('mouseover', function(event){
  51. var target = event.target;
  52. if((target.className
  53. && !!~target.className.indexOf('j_user_card' )
  54. && target.nodeName == 'A') ||
  55. ucp.contains(target)){
  56. event.stopPropagation();
  57. clearTimeout(hideCardTimeout);
  58. if(ucp.contains(target)) return;
  59. clearTimeout(showCardTimeout);
  60. showCardTimeout = setTimeout(function(){
  61. var rects = target.getBoundingClientRect(),
  62. scrollTop = document.documentElement.scrollTop || document.body.scrollTop,
  63. card = getUserCard(
  64. JSON.parse(target.dataset.field.replace(/'/g,'"')).un
  65. || decodeURIComponent(target.href.match(/un\=([^&]+)/)[1]));
  66.  
  67. for(var i in cardList){
  68. if(cardList[i] != card)
  69. cardList[i].style.display = 'none';
  70. }
  71. card.style.display = ucp.style.display = 'block';
  72.  
  73. if(/at|lzl_p_p/.test(target.className) || window.hasOwnProperty('frsPage')){
  74. if(rects.top > 140 + 20){
  75. ucp.style.top = scrollTop + rects.top - 140 - rects.height + 'px';
  76. }else{
  77. ucp.style.top = scrollTop + rects.top + rects.height + 5 + 'px';
  78. }
  79. ucp.style.left = rects.left + 'px';
  80. }else{
  81. ucp.style.top = scrollTop + rects.top - 60 + 'px';
  82. ucp.style.left = rects.right + 20 + 'px';
  83. }
  84. }, 350);
  85. }
  86. }, true);
  87.  
  88. document.addEventListener('mouseout', function(event){
  89. var target = event.target;
  90. if((target.className
  91. && !!~target.className.indexOf('j_user_card')
  92. && target.nodeName == 'A')
  93. || ucp.contains(target)
  94. ){
  95. clearTimeout(showCardTimeout);
  96. clearTimeout(hideCardTimeout);
  97. hideCardTimeout = setTimeout(function(){
  98. ucp.style.display = 'none';
  99. }, 200);
  100. }
  101. }, true);
  102.  
  103. function getUserCard(userName){
  104. if(userName in cardList)
  105. return cardList[userName];
  106.  
  107. var iframe = document.createElement('iframe');
  108. iframe.src = '/i/data/panel?ie=utf-8&un=' + encodeURIComponent(userName);
  109. iframe.style.width = '100%';
  110. cardList[userName] = iframe;
  111. ucp.appendChild(iframe);
  112. iframe.contentWindow.addEventListener('DOMContentLoaded', function(){
  113. var msgId = new Date().getTime();
  114. addEventListener('message', function setUserCard(e){
  115. if(e.data.name !== 'tiebaOldUserCard' || e.data.msgId !== msgId || !e.data.uk)
  116. return;
  117. removeEventListener('message', setUserCard, false);
  118. iframe.contentWindow.document.getElementById('icon_right').insertAdjacentHTML('beforeend',
  119. (e.data.uk === 'none' ? '<small>对方隐藏了相册</small>,' :
  120. ' <a target="_blank" href="http://yun.baidu.com/share/home?uk='
  121. + e.data.uk +'">盘</a>,'
  122. + '<a target="_blank" href="http://xiangce.baidu.com/u/'
  123. + e.data.uk +'">册</a>,')
  124.  
  125. + '<a target="_blank" href="http://www.tieba.com/f/search/ures?ie=utf-8&kw=&qw=&rn=100&sm=0&un='
  126. + userName +'">黑</a>');
  127. }, false);
  128.  
  129. crossFrame.contentWindow.postMessage({
  130. name: 'tiebaOldUserCard',
  131. user: userName,
  132. msgId: msgId,
  133. }, crossFrameUrl);
  134.  
  135. });
  136. return iframe;
  137. }
  138. })();