fix zhihu imagepreview 知乎图片查看

知乎图片查看

目前为 2019-08-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name fix zhihu imagepreview 知乎图片查看
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.51
  5. // @description 知乎图片查看
  6. // @author You
  7. // @match https://www.zhihu.com/question/*
  8. // @match https://zhuanlan.zhihu.com/p/*
  9. // @require https://cdn.bootcss.com/document-register-element/1.11.1/document-register-element.js
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. let cssprop = function createCss({rotate}) {
  17. return {
  18. transform: `translate(-50%) rotate(${rotate})`,
  19. position: 'relative',
  20. left: '50%',
  21. top: '20px',
  22. cursor: 'default',
  23. }
  24. }
  25.  
  26. function Ztest(value = 0) {
  27. let img = document.querySelector('.ImageView img')
  28. img.parentNode.style.overflow = "auto"
  29. let pro = cssprop({
  30. rotate: value + 'deg'
  31. })
  32. for (let key in pro) {
  33. img.style[key] = pro[key]
  34. }
  35. }
  36.  
  37. class MyControl extends HTMLElement {
  38. constructor() {
  39. super()
  40. // Attach a shadow root to the element.
  41. let shadowRoot = this.attachShadow({mode: 'open'});
  42. shadowRoot.innerHTML = `
  43. <style>
  44. :host { display: block; position: fixed; right: 20px; top: 50%; width: 120px; transform: translate(0, -50%); z-index: 10000; color: #fff; }
  45. :host img { max-width: 50px }
  46. :host input { width: 30px }
  47. </style>
  48. <div>
  49. <input type="range" value="0" step="5" min="0" max="360" style="width: 120px"/>
  50. <div>度</div>
  51. </div>
  52. <div>
  53. <input type="range" value="20" step="1" min="0" max="1000" style="width: 120px"/>
  54. <div>top</div>
  55. </div>
  56. <div>
  57. <input type="range" value="1" step="0.1" min="0" max="3" style="width: 120px"/>
  58. <div>放大</div>
  59. </div>
  60. `;
  61. shadowRoot.children[1].children[0].addEventListener('input', (e) => {
  62. let _value = Number.parseFloat(e.target.value)
  63. if (!Number.isNaN(_value)) {
  64. shadowRoot.children[1].children[1].textContent = '角度: ' +_value + '度'
  65. let _transform = this.img.style.transform
  66. this.img.style.transform = _transform.replace(/rotate\(\w+\)/g, 'rotate('+_value+'deg)')
  67. }
  68. })
  69.  
  70. shadowRoot.children[2].children[0].addEventListener('input', (e) => {
  71. let _value = Number.parseFloat(e.target.value)
  72. shadowRoot.children[2].children[1].textContent = 'top: ' +_value + '高'
  73. this.img.style.top = _value + 'px'
  74. })
  75. shadowRoot.children[3].children[0].addEventListener('input', (e) => {
  76. let _value = Number.parseFloat(e.target.value)
  77. shadowRoot.children[3].children[1].textContent = '倍数: ' + _value + '倍'
  78. this.img.style.width = (this.imgWidth * _value) + 'px'
  79. })
  80. this._shadowRoot = shadowRoot
  81. }
  82.  
  83. show() {
  84. let degree = 0
  85. let top = 20
  86. let bei = 2
  87. this.img = document.querySelector('.ImageView img')
  88. this.imgWidth = this.img.clientWidth
  89. this._shadowRoot.children[1].style.display = null
  90. this._shadowRoot.children[2].style.display = null
  91. this._shadowRoot.children[3].style.display = null
  92. Ztest(0)
  93. this._shadowRoot.children[1].children[0].value = degree
  94. this._shadowRoot.children[1].children[1].textContent = '角度: ' + degree + '度'
  95. this._shadowRoot.children[2].children[0].value = top
  96. this._shadowRoot.children[2].children[1].textContent = 'top: ' + top + '高'
  97. this._shadowRoot.children[3].children[0].value = bei
  98. this._shadowRoot.children[3].children[1].textContent = '倍数: ' + bei + '倍'
  99.  
  100. this.img.style.width = (this.imgWidth * bei) + 'px'
  101. }
  102.  
  103. hide() {
  104. this._shadowRoot.children[1].style.display = 'none'
  105. this._shadowRoot.children[2].style.display = 'none'
  106. this._shadowRoot.children[3].style.display = 'none'
  107. }
  108. }
  109.  
  110. customElements.define('my-control', MyControl)
  111.  
  112. let mycontrol = new MyControl()
  113.  
  114.  
  115. var targetNode = document.body
  116. var config = { attributes: false, childList: true, subtree: true };
  117.  
  118. var callback = function(mutationsList) {
  119. for(var mutation of mutationsList) {
  120. // console.log(mutation
  121. if (mutation.addedNodes && mutation.addedNodes[0] && mutation.addedNodes[0].innerHTML.indexOf('ImageView') > -1 ) {
  122. console.log("bang")
  123. setTimeout(function() {
  124. mycontrol.show()
  125. Ztest()
  126. }, 0)
  127. }
  128.  
  129. if (mutation.removedNodes && mutation.removedNodes[0] && mutation.removedNodes[0].innerHTML.indexOf('ImageView') > -1 ) {
  130. setTimeout(function() {
  131. mycontrol.hide()
  132.  
  133. }, 0)
  134. }
  135. }
  136. };
  137.  
  138. var observer = new MutationObserver(callback);
  139.  
  140. observer.observe(targetNode, config);
  141.  
  142.  
  143. document.body.appendChild(mycontrol)
  144. mycontrol.hide()
  145. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址