fix zhihu imagepreview 知乎图片查看

try to take over the world!

目前为 2018-10-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name fix zhihu imagepreview 知乎图片查看
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.31
  5. // @description try to take over the world!
  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="2" 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. this.img = document.querySelector('.ImageView img')
  85. this.imgWidth = this.img.clientWidth
  86. this._shadowRoot.children[1].style.display = null
  87. this._shadowRoot.children[2].style.display = null
  88. this._shadowRoot.children[3].style.display = null
  89. Ztest(0)
  90. this._shadowRoot.children[1].children[0].value = 0
  91. this._shadowRoot.children[1].children[1].textContent = '角度: ' + 0 + '度'
  92. this._shadowRoot.children[2].children[0].value = 20
  93. this._shadowRoot.children[2].children[1].textContent = 'top: ' +20 + '高'
  94. this._shadowRoot.children[3].children[0].value = 1
  95. this._shadowRoot.children[3].children[1].textContent = '倍数: ' + 1 + '倍'
  96. }
  97.  
  98. hide() {
  99. this._shadowRoot.children[1].style.display = 'none'
  100. this._shadowRoot.children[2].style.display = 'none'
  101. this._shadowRoot.children[3].style.display = 'none'
  102. }
  103. }
  104.  
  105. customElements.define('my-control', MyControl)
  106.  
  107. let mycontrol = new MyControl()
  108.  
  109.  
  110. var targetNode = document.body
  111. var config = { attributes: false, childList: true, subtree: true };
  112.  
  113. var callback = function(mutationsList) {
  114. for(var mutation of mutationsList) {
  115. // console.log(mutation
  116. if (mutation.addedNodes && mutation.addedNodes[0] && mutation.addedNodes[0].innerHTML.indexOf('ImageView') > -1 ) {
  117. console.log("bang")
  118. setTimeout(function() {
  119. mycontrol.show()
  120. Ztest()
  121. }, 0)
  122. }
  123.  
  124. if (mutation.removedNodes && mutation.removedNodes[0] && mutation.removedNodes[0].innerHTML.indexOf('ImageView') > -1 ) {
  125. setTimeout(function() {
  126. mycontrol.hide()
  127.  
  128. }, 0)
  129. }
  130. }
  131. };
  132.  
  133. var observer = new MutationObserver(callback);
  134.  
  135. observer.observe(targetNode, config);
  136.  
  137.  
  138. document.body.appendChild(mycontrol)
  139. mycontrol.hide()
  140. })();

QingJ © 2025

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