XKCD tooltip

This script puts the xkcd tooltip under the picture and adds a link to explainxkcd.com

当前为 2015-01-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name XKCD tooltip
  3. // @namespace http://userscripts.org/users/lorentz
  4. // @description This script puts the xkcd tooltip under the picture and adds a link to explainxkcd.com
  5. // @include http://xkcd.com/*
  6. // @include http://www.xkcd.com/*
  7. // @include https://www.xkcd.com/*
  8. // @include http://what-if.xkcd.com/*
  9. // @grant none
  10. // @version 1.1
  11. // @icon https://raw.githubusercontent.com/Lorentz83/userscripts/master/XKCDTooltip/icon.png
  12. // ==/UserScript==
  13.  
  14. /**
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. var addAfter = function (dom, newNode){
  29. dom.parentNode.insertBefore(newNode, dom.nextSibling);
  30. }
  31.  
  32. var addTitleBox = function(img, after) {
  33. var title = img.title;
  34. if(title.length == 0)
  35. return;
  36. //img.title='';
  37. var titleBox = document.createElement('div');
  38. titleBox.innerHTML = title;
  39. titleBox.style.backgroundColor='#FFF36F';
  40. titleBox.style.fontVariant='normal';
  41. titleBox.style.border='1px solid #7F7F7F';
  42. titleBox.style.padding='2px';
  43. titleBox.style.width='60%';
  44. titleBox.style.margin='auto';
  45. titleBox.style.fontSize='70%';
  46. addAfter(after,titleBox);
  47. return titleBox;
  48. }
  49.  
  50. window.onload = function() {
  51. var comicBox = document.getElementById('comic');
  52. if (comicBox) {
  53. var img = comicBox.getElementsByTagName('img')[0];
  54. var titleBox = addTitleBox(img,comicBox);
  55. var name = document.getElementById('ctitle').innerHTML;
  56. var a = document.createElement('a');
  57. var id = document.location.href.split('/')[3];
  58. a.href = 'http://www.explainxkcd.com/wiki/index.php?title=' + id;
  59. a.innerHTML = 'explain this';
  60. addAfter(titleBox,a);
  61. }
  62. var article = document.getElementsByTagName('article');
  63. if(article.length > 0){
  64. var imgs = article[0].getElementsByTagName('img');
  65. for (var i =0 ; i<imgs.length ; i++){
  66. addTitleBox(imgs[i], imgs[i]);
  67. }
  68. }
  69.  
  70. };