Ylilauta reverse image search button

Adds a button for reverse image search

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name Ylilauta reverse image search button
// @namespace Violentmonkey Scripts
// @match *://ylilauta.org/*
// @grant none
// @version 0.4
// @locale en
// @description Adds a button for reverse image search
// ==/UserScript==

const addSearchButtons = () => {
  const posts = Array.from(document.querySelectorAll('div.op_post, div.answer'))
    .map(div => {
      const post = Array.from(div.childNodes).find(n => n.className === 'post')
      const filecontainer = Array.from(post.childNodes).find(
        n => n.className && n.className.indexOf('filecontainer thumbnail file') !== -1
      )
      if (filecontainer === undefined) return
      const expandlink = Array.from(filecontainer.childNodes).find(n => n.className === 'expandlink')
      if (expandlink === undefined) return
      const href = expandlink.href
      if (href === undefined) return
      const postinfo = Array.from(div.childNodes).find(n => n.className === 'postinfo')
      const messageoptions = Array.from(postinfo.childNodes).find(n => n.className === 'messageoptions')
      const magnifier = Array.from(messageoptions.childNodes).find(n => n.className === 'icon-magnifier')
      if (magnifier === undefined) {
        const link = document.createElement('a')
        link.className = 'icon-magnifier'
        link.href = 'https://images.google.com/searchbyimage?image_url=' + href
        link.title = 'Käänteinen kuvahaku'
        link.target = '_blank'
        messageoptions.insertBefore(link, messageoptions.children[1])
      }
    })
}

addSearchButtons()

const targetDiv = document.querySelector('div.answers')
const config = { childList: true }

const observer = new MutationObserver(
  (mutationsList) => {
    if (Array.from(mutationsList).filter(
      (mutation) => mutation.type === 'childList').length > 0) {
      addSearchButtons()
    }
  }
)

observer.observe(targetDiv, config)