Wiki file link preview

preview image for link that start with "File:"

  1. // ==UserScript==
  2. // @name Wiki file link preview
  3. // @namespace https://unlucky.ninja/
  4. // @version 0.1
  5. // @description preview image for link that start with "File:"
  6. // @author UnluckyNinja
  7. // @match https://*.wikipedia.org/*
  8. // @require https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js
  9. // @require https://greasyfork.org/scripts/2199-waitforkeyelements/code/waitForKeyElements.js?version=6349
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_addStyle
  12. // ==/UserScript==
  13.  
  14. // (function($, undefined) {
  15. (function() {
  16. 'use strict';
  17.  
  18. $(function() {
  19. GM_addStyle(`
  20. #img-preview {
  21. position: fixed;
  22. width: auto;
  23. height: auto;
  24. }
  25. .wip-upper-right {
  26. top: 10px;
  27. right: 10px;
  28. }
  29. .wip-upper-left {
  30. top: 10px;
  31. left: 10px;
  32. }
  33. `)
  34. let preview = $('<img id="img-preview"></img>')
  35.  
  36. function checkMouse(event, img) {
  37. console.log(event.clientX + ', ' + window.innerWidth)
  38. if (event.clientX < window.innerWidth / 2) {
  39. img.removeClass('wip-upper-left')
  40. img.addClass('wip-upper-right')
  41. } else {
  42. img.removeClass('wip-upper-right')
  43. img.addClass('wip-upper-left')
  44. }
  45. }
  46.  
  47. let m_in = (event) => {
  48. console.log('m_in in ' + $(event.target).toString())
  49. let img = $('#img-preview')
  50. checkMouse(event, img)
  51. let imglink = '';
  52. let url = $(event.target).attr('href')
  53. if (url && url !== '') {
  54. console.log('url is ' + url)
  55. $.post(url, null, function(data, status, jqxhr) {
  56. imglink = $(data).find('#file img').first().attr('src');
  57. if (img.is(':visible')) {
  58. img.attr('src', imglink)
  59. }
  60. console.log('post success, imglink:' + imglink)
  61. }).fail(() => { console.log('get image failed') })
  62. }
  63. img.show()
  64. }
  65.  
  66. let m_out = (event) => {
  67. let img = $('#img-preview')
  68. img.hide()
  69. img.attr('src', null)
  70. }
  71.  
  72. preview.appendTo('body')
  73. preview.hide()
  74.  
  75. let addPreviewEvent = function(node) {
  76. if (node.text().startsWith('File:')) {
  77. node.hover(m_in, m_out)
  78. }
  79. }
  80.  
  81. waitForKeyElements("#mw-category-media a", addPreviewEvent);
  82. })
  83. })()
  84. //})(window.jQuery.noConflict(true) || $); // not work