Image Search Script

长按滑鼠右键呼叫图片搜寻选单,提供流畅且简洁的使用体验。

目前为 2024-11-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Image Search Script
  3. // @name:zh-TW Image Search Script
  4. // @name:zh-CN Image Search Script
  5. // @namespace https://github.com/Pixmi/image-search-script
  6. // @version 1.1.7
  7. // @description Long-pressing the right mouse button brings up the image search menu, offering a smooth and concise user experience.
  8. // @description:zh-TW 長按滑鼠右鍵呼叫圖片搜尋選單,提供流暢且簡潔的使用體驗。
  9. // @description:zh-CN 长按滑鼠右键呼叫图片搜寻选单,提供流畅且简洁的使用体验。
  10. // @author Pixmi
  11. // @homepage https://github.com/Pixmi/image-search-script
  12. // @supportURL https://github.com/Pixmi/image-search-script/issues
  13. // @icon https://raw.githubusercontent.com/Pixmi/image-search-script/main/icon.svg
  14. // @match *://*/*
  15. // @require https://openuserjs.org/src/libs/sizzle/GM_config.js
  16. // @grant GM_setValue
  17. // @grant GM_getValue
  18. // @grant GM_addStyle
  19. // @grant GM_registerMenuCommand
  20. // @grant GM_xmlhttpRequest
  21. // @connect ascii2d.net
  22. // @license GPL-3.0
  23. // @run-at document-body
  24. // @noframes
  25. // ==/UserScript==
  26.  
  27. GM_addStyle(`
  28. @keyframes fadeIn {
  29. 0% { opacity: 0; }
  30. 100% { opacity: 1; }
  31. }
  32. @keyframes fadeOut {
  33. 0% { opacity: 1; }
  34. 100% { opacity: 0; }
  35. }
  36. #image-search-menu {
  37. animation: fadeOut 200ms ease-in-out forwards;
  38. background-color: rgba(0, 0, 0, .75);
  39. color: rgb(255, 255, 255);
  40. display: none;
  41. flex-direction: column;
  42. font-size: 16px;
  43. width: unset;
  44. min-width: 150px;
  45. height: unset;
  46. min-height: unset;
  47. position: fixed;
  48. top: unset;
  49. left: unset;
  50. z-index: 9999;
  51. }
  52. #image-search-menu.show {
  53. animation: fadeIn 200ms ease-in-out forwards;
  54. display: flex;
  55. }
  56. .image-search-option {
  57. cursor: pointer;
  58. display: block;
  59. padding: 5px 10px;
  60. }
  61. .image-search-option + .image-search-option {
  62. border-top: 1px solid rgba(255, 255, 255, .5);
  63. }
  64. .image-search-option:hover {
  65. background-color: rgba(255, 255, 255, .3);
  66. }
  67. iframe#image-search-setting {
  68. width: 350px !important;
  69. height: 500px !important;
  70. }
  71. `);
  72.  
  73. const searchOptions = new Map([
  74. {
  75. label: 'Google Lens',
  76. key: 'GOOGLE_LENS',
  77. url: 'https://lens.google.com/uploadbyurl?url=%s'
  78. }, {
  79. label: 'SauceNAO',
  80. key: 'SAUCENAO',
  81. url: 'https://saucenao.com/search.php?url=%s'
  82. }, {
  83. label: 'Ascii2D',
  84. key: 'ASCII2D',
  85. url: ''
  86. }, {
  87. label: 'IQDB',
  88. key: 'IQDB',
  89. url: 'https://iqdb.org/?url=%s'
  90. }, {
  91. label: 'TinEye',
  92. key: 'TINEYE',
  93. url: 'https://www.tineye.com/search?url=%s'
  94. }, {
  95. label: 'Baidu',
  96. key: 'BAIDU',
  97. url: 'https://image.baidu.com/n/pc_search?queryImageUrl=%s'
  98. }, {
  99. label: 'Bing',
  100. key: 'BING',
  101. url: 'https://www.bing.com/images/searchbyimage?FORM=IRSBIQ&cbir=sbi&imgurl=%s'
  102. }
  103. ].map(item => [item.key, item]));
  104.  
  105. (function () {
  106. 'use strict';
  107.  
  108. const hoverOpen = {
  109. enabled: GM_getValue('HOVER_OPEN', false),
  110. minWidth: GM_getValue('HOVER_OPEN_MIN_WIDTH', 100),
  111. minHeight: GM_getValue('HOVER_OPEN_MIN_HEIGHT', 100)
  112. };
  113.  
  114. const newTab = (url) => {
  115. const tab = document.createElement('a');
  116. tab.href = url;
  117. tab.dispatchEvent(new MouseEvent('click', {ctrlKey: true, metaKey: true}));
  118. };
  119.  
  120. const hoverCheck = (event) => {
  121. const { target, relatedTarget } = event;
  122. if (target.className == 'image-search-option' && relatedTarget == searchMenu.image) {
  123. return true;
  124. }
  125. if (target.tagName === 'IMG' && target.width >= hoverOpen.minWidth && target.height >= hoverOpen.minHeight) {
  126. return true;
  127. }
  128. return false;
  129. };
  130.  
  131. document.addEventListener('mouseover', (event) => {
  132. if (hoverOpen.enabled) {
  133. if (hoverCheck(event)) {
  134. searchMenu.image = event.relatedTarget;
  135. searchMenu.open(event.target);
  136. } else {
  137. searchMenu.clear();
  138. }
  139. }
  140. });
  141.  
  142. document.addEventListener('mousedown', (event) => {
  143. searchMenu.holding = false;
  144. if (event.button === 2 && event.target.nodeName === 'IMG') {
  145. searchMenu.timer = setTimeout(() => {
  146. searchMenu.holding = true;
  147. searchMenu.open(event.target);
  148. }, 200);
  149. } else {
  150. if (event.target !== searchMenu.pane && !event.target.classList.contains('image-search-option')) {
  151. searchMenu.clear();
  152. }
  153. }
  154. });
  155.  
  156. document.addEventListener('mouseup', (event) => {
  157. if (event.button === 2) {
  158. clearTimeout(searchMenu.timer);
  159. if (searchMenu.holding) {
  160. event.preventDefault();
  161. }
  162. }
  163. });
  164.  
  165. document.addEventListener('contextmenu', (event) => {
  166. if (searchMenu.holding) {
  167. event.preventDefault();
  168. } else {
  169. searchMenu.clear();
  170. }
  171. });
  172.  
  173. document.addEventListener('visibilitychange', () => {
  174. if (document.visibilityState === 'visible') {
  175. searchMenu.update();
  176. }
  177. });
  178.  
  179. document.addEventListener('scroll', () => { searchMenu.update(); });
  180. window.addEventListener('resize', () => { searchMenu.update(); });
  181.  
  182. class searchMenuController {
  183. constructor() {
  184. this.panel = null;
  185. this.image = null;
  186. this.holding = false;
  187. this.timer = null;
  188. this.init();
  189. }
  190.  
  191. init() {
  192. this.panel = document.createElement('div');
  193. this.panel.id = 'image-search-menu';
  194. this.panel.addEventListener('click', (event) => {
  195. const action = event.target.dataset.action || false;
  196. if (action) {
  197. switch (action) {
  198. case 'ASCII2D':
  199. GM_xmlhttpRequest({
  200. method: 'POST',
  201. url: 'https://ascii2d.net/imagesearch/search/',
  202. data: JSON.stringify({ uri: this.image.src }),
  203. headers: {
  204. 'Content-Type': 'application/json',
  205. },
  206. timeout: 10000,
  207. onload: function(response) {
  208. if (response.status == 200) {
  209. newTab(response.finalUrl);
  210. }
  211. },
  212. onerror: function(error) {
  213. console.error('請求錯誤:', error);
  214. },
  215. ontimeout: function() {
  216. console.error('請求超時');
  217. }
  218. });
  219. break;
  220. default: {
  221. const option = searchOptions.get(action) || false;
  222. if (!option) break;
  223. newTab(option.url.replace('%s', this.image.src));
  224. break;
  225. }
  226. }
  227. }
  228. this.clear();
  229. });
  230. document.body.append(this.panel);
  231. }
  232.  
  233. open(target) {
  234. if (target.nodeName === 'IMG') {
  235. while (this.panel.hasChildNodes()) { this.panel.lastChild.remove(); }
  236. for (const [key, option] of searchOptions) {
  237. if (!GM_getValue(key, true)) continue;
  238. const item = document.createElement('div');
  239. item.className = 'image-search-option';
  240. item.textContent = option.label;
  241. item.dataset.action = key;
  242. this.panel.append((item));
  243. }
  244. this.image = target;
  245. this.update();
  246. this.panel.classList.add('show');
  247. }
  248. }
  249.  
  250. update() {
  251. if (this.image) {
  252. const status = {
  253. width: this.image.width,
  254. left: this.image.x,
  255. top: this.image.y
  256. };
  257. for (const key of Object.keys(status)) {
  258. this.panel.style[key] = `${status[key]}px`;
  259. }
  260. }
  261. }
  262.  
  263. clear() {
  264. this.image = null;
  265. this.panel.classList.remove('show');
  266. this.panel.style.width = 0;
  267. this.panel.style.left = 0;
  268. this.panel.style.top = 0;
  269. }
  270. }
  271.  
  272. const searchMenu = new searchMenuController();
  273.  
  274. GM_registerMenuCommand('Setting', () => config.open());
  275.  
  276. const config = new GM_config({
  277. 'id': 'image-search-setting',
  278. 'css': `
  279. #image-search-setting * {
  280. box-sizing: border-box;
  281. }
  282. #image-search-setting {
  283. box-sizing: border-box;
  284. width: 100%;
  285. height: 100%;
  286. padding: 10px;
  287. margin: 0;
  288. }
  289. #image-search-setting_wrapper {
  290. display: flex;
  291. flex-direction: column;
  292. height: 100%;
  293. }
  294. #image-search-setting_buttons_holder {
  295. text-align: center;
  296. margin-top: auto;
  297. }
  298. .config_var {
  299. margin: 5px 0 !important;
  300. }
  301. .field_label {
  302. font-size: 14px !important;
  303. }
  304. `,
  305. 'title': 'Image Search Setting',
  306. 'fields': {
  307. 'GOOGLE_LENS': {
  308. 'type': 'checkbox',
  309. 'label': 'Google Lens',
  310. 'section': ['Search Options'],
  311. 'default': true,
  312. },
  313. 'SAUCENAO': {
  314. 'type': 'checkbox',
  315. 'label': 'SauceNAO',
  316. 'default': true,
  317. },
  318. 'ASCII2D': {
  319. 'type': 'checkbox',
  320. 'label': 'Ascii2D',
  321. 'default': true,
  322. },
  323. 'IQDB': {
  324. 'type': 'checkbox',
  325. 'label': 'IQDB',
  326. 'default': true,
  327. },
  328. 'TINEYE': {
  329. 'type': 'checkbox',
  330. 'label': 'TinEye',
  331. 'default': true,
  332. },
  333. 'BAIDU': {
  334. 'type': 'checkbox',
  335. 'label': 'Baidu',
  336. 'default': true,
  337. },
  338. 'BING': {
  339. 'type': 'checkbox',
  340. 'label': 'Bing',
  341. 'default': true,
  342. },
  343. 'HOVER_OPEN': {
  344. 'type': 'checkbox',
  345. 'label': 'Enabled hover open',
  346. 'section': ['Hover images to open menu'],
  347. 'default': false,
  348. },
  349. 'HOVER_OPEN_MIN_WIDTH': {
  350. 'label': 'Image min width (px)',
  351. 'type': 'int',
  352. 'default': 100,
  353. },
  354. 'HOVER_OPEN_MIN_HEIGHT': {
  355. 'label': 'Image min height (px)',
  356. 'type': 'int',
  357. 'default': 100,
  358. }
  359. },
  360. 'events': {
  361. 'init': () => {
  362. for (const [key] of searchOptions) { config.set(key, GM_getValue(key, true)); }
  363. config.set('HOVER_OPEN', GM_getValue('HOVER_OPEN', false));
  364. config.set('HOVER_OPEN_MIN_WIDTH', GM_getValue('HOVER_OPEN_MIN_WIDTH', 100));
  365. config.set('HOVER_OPEN_MIN_HEIGHT', GM_getValue('HOVER_OPEN_MIN_HEIGHT', 100));
  366. },
  367. 'save': () => {
  368. for (const [key] of searchOptions) { GM_setValue(key, config.get(key)); }
  369. GM_setValue('HOVER_OPEN', config.get('HOVER_OPEN'));
  370. GM_setValue('HOVER_OPEN_MIN_WIDTH', config.get('HOVER_OPEN_MIN_WIDTH'));
  371. GM_setValue('HOVER_OPEN_MIN_HEIGHT', config.get('HOVER_OPEN_MIN_HEIGHT'));
  372. hoverOpen.enabled = config.get('HOVER_OPEN');
  373. hoverOpen.minWidth = config.get('HOVER_OPEN_MIN_WIDTH');
  374. hoverOpen.minHeight = config.get('HOVER_OPEN_MIN_HEIGHT');
  375. config.close();
  376. }
  377. }
  378. });
  379. })();