SearX: Link to proxified image

changes original href link of the image to proxified. Instances list updates automatically every 2 weeks

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name SearX: Link to proxified image
// @namespace -
// @version 1.1.0
// @description changes original href link of the image to proxified. Instances list updates automatically every 2 weeks
// @author NotYou
// @match *://*/*
// @grant GM.xmlHttpRequest
// @grant GM.setValue
// @grant GM.getValue
// @connect searx.space
// @run-at document-start
// @license GPL-3.0
// ==/UserScript==

!async function () {
    'use strict';

    const getLinkNode = node => {
        if (node.tagName.toLowerCase() === 'a') {
            return node
        } else if (node.tagName.toLowerCase() === 'html' || !node.tagName) {
            return null
        }

        return getLinkNode(node.parentNode)
    }

    const getInstances = async () => {
        return new Promise(async (resolve, reject) => {
            const instances = await GM.getValue('instances', {})
            const instancesLastUpdate = await GM.getValue('instancesLastUpdate', '1970-01-01T00:00:00.000Z')
            const instancesLastUpdateDate = new Date(instancesLastUpdate)

            instancesLastUpdateDate.setDate(instancesLastUpdateDate.getDate() + 14)

            const currentDate = new Date()

            if (currentDate > instancesLastUpdateDate) {
                GM.xmlHttpRequest({
                    url: 'https://searx.space/data/instances.json',
                    responseType: 'json',
                    onload: async ({ response }) => {
                        const instances = Object.keys(response.instances)

                        await GM.setValue('instances', instances)
                        await GM.setValue('instancesLastUpdate', currentDate.toJSON())

                        resolve(instances)
                    },
                    onerror: reject
                })
            } else {
                return resolve(null)
            }
        })
    }

    const instances = await getInstances()

    if (instances === null) return

    const foundHost = instances.find(instance => instance.includes(location.host))

    if (typeof foundHost === 'undefined') return

    document.querySelectorAll('.img-thumbnail, .image-thumbnail, .image_thumbnail').forEach($imageThumb => {
        const $link = getLinkNode($imageThumb)

        if ($link === null) return

        $link.href = $imageThumb.src
    })
}()