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.

目前為 2023-04-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         OCR Image Content Display
// @namespace    http://tampermonkey.net/
// @version      1.1.1
// @description  Displays the text content of the image in the clipboard using OCR and displays it in an alert when the script is run.
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

       async function displayImageContent() {

        const clipboardItems = await navigator.clipboard.read();
        for (const clipboardItem of clipboardItems) {
            for (const type of clipboardItem.types) {
                if (type.startsWith('image/')) {
                    const blob = await clipboardItem.getType(type);
                    const formData = new FormData();
                    formData.append('apikey', '你自己的 api');
                    formData.append('language', 'eng');
                    formData.append('isOverlayRequired', 'false');
                    formData.append('file', blob, 'image.png');
                    const response = await fetch('https://api.ocr.space/parse/image', {
                        method: 'POST',
                        body: formData
                    });
                    const data = await response.json();
                    if (data && data.ParsedResults && data.ParsedResults.length > 0) {
                        const text = data.ParsedResults[0].ParsedText;
                        // 将识别结果显示在一个提示框中
                        // 调用 OpenAI 的翻译服务将识别结果翻译成中文
 const audio = new Audio(`https://tsn.baidu.com/text2audio?tex=${text}&lan=zh&cuid=abcdefg1234567&ctp=1&per=5003&tok=你自己的 tok`);
                        const apiKey = "你自己的 api";
                        const openaiResponse = await fetch('https://api.openai.com/v1/engines/text-davinci-003/completions', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                                'Authorization': `Bearer ${apiKey}`
                            },
                            body: JSON.stringify({
                                prompt: `Translate the following English text into Chinese:\n\n${text}.`,
                                max_tokens: 1000,
                                n: 1,
                                temperature: 0.9
                            })
                        });
                        const openaiData = await openaiResponse.json();
                        const translation = openaiData.choices[0].text;
                        // 将识别结果和翻译结果显示在一个提示框中
                        alert(`${text}${translation}`);
audio.play();
                    }
                }
            }
        }


       }

    const cursor = document.createElement('button');
    cursor.style.position = 'fixed';
    cursor.style.top = '10%';
    cursor.style.right = '10%';
    cursor.style.width = '50px';
    cursor.style.height = '50px';
    cursor.style.backgroundColor = '#b1c5b4';
    cursor.style.borderRadius = '50%';
    //cursor.style.cursor = 'pointer';
    cursor.style.boxShadow = '0 8 24px rgba(0, 0, 0, 0.15)';
    cursor.innerText = '👻';
    cursor.style.fontSize = '20px';
    cursor.style.border = "2px solid #888888";
    cursor.style.zIndex = '9999';

    document.body.appendChild(cursor);


    cursor.addEventListener('click', displayImageContent);
})()