OCR Image Content Display

Displays the text content of the image in the clipboard using OCR and displays it in an alert when the script is run.

  1. // ==UserScript==
  2. // @name OCR Image Content Display
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1.1
  5. // @description Displays the text content of the image in the clipboard using OCR and displays it in an alert when the script is run.
  6. // @match *://*/*
  7. // @grant none
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. async function displayImageContent() {
  15.  
  16. const clipboardItems = await navigator.clipboard.read();
  17. for (const clipboardItem of clipboardItems) {
  18. for (const type of clipboardItem.types) {
  19. if (type.startsWith('image/')) {
  20. const blob = await clipboardItem.getType(type);
  21. const formData = new FormData();
  22. formData.append('apikey', '你自己的 api');
  23. formData.append('language', 'eng');
  24. formData.append('isOverlayRequired', 'false');
  25. formData.append('file', blob, 'image.png');
  26. const response = await fetch('https://api.ocr.space/parse/image', {
  27. method: 'POST',
  28. body: formData
  29. });
  30. const data = await response.json();
  31. if (data && data.ParsedResults && data.ParsedResults.length > 0) {
  32. const text = data.ParsedResults[0].ParsedText;
  33. // 将识别结果显示在一个提示框中
  34. // 调用 OpenAI 的翻译服务将识别结果翻译成中文
  35. const audio = new Audio(`https://tsn.baidu.com/text2audio?tex=${text}&lan=zh&cuid=abcdefg1234567&ctp=1&per=5003&tok=你自己的 tok`);
  36. const apiKey = "你自己的 api";
  37. const openaiResponse = await fetch('https://api.openai.com/v1/engines/text-davinci-003/completions', {
  38. method: 'POST',
  39. headers: {
  40. 'Content-Type': 'application/json',
  41. 'Authorization': `Bearer ${apiKey}`
  42. },
  43. body: JSON.stringify({
  44. prompt: `Translate the following English text into Chinese:\n\n${text}.`,
  45. max_tokens: 1000,
  46. n: 1,
  47. temperature: 0.9
  48. })
  49. });
  50. const openaiData = await openaiResponse.json();
  51. const translation = openaiData.choices[0].text;
  52. // 将识别结果和翻译结果显示在一个提示框中
  53. alert(`${text}${translation}`);
  54. audio.play();
  55. }
  56. }
  57. }
  58. }
  59.  
  60.  
  61. }
  62.  
  63. const cursor = document.createElement('button');
  64. cursor.style.position = 'fixed';
  65. cursor.style.top = '10%';
  66. cursor.style.right = '10%';
  67. cursor.style.width = '50px';
  68. cursor.style.height = '50px';
  69. cursor.style.backgroundColor = '#b1c5b4';
  70. cursor.style.borderRadius = '50%';
  71. //cursor.style.cursor = 'pointer';
  72. cursor.style.boxShadow = '0 8 24px rgba(0, 0, 0, 0.15)';
  73. cursor.innerText = '👻';
  74. cursor.style.fontSize = '20px';
  75. cursor.style.border = "2px solid #888888";
  76. cursor.style.zIndex = '9999';
  77.  
  78. document.body.appendChild(cursor);
  79.  
  80.  
  81. cursor.addEventListener('click', displayImageContent);
  82. })()