ssborbis / ContextSearch-web-ext

Search engine manager for modern browsers
329 stars 37 forks source link

Automatically load title links after search #592

Closed Parvares closed 1 year ago

Parvares commented 1 year ago

Hi Mike, I'm trying to use your extension with this template URL:

https://www.bibliotechediroma.it/opac/query/%s?context=tmatm

(where %s is to be replacede by the search key, for example Stephen King). I would like all the 15 resulting titles/records to be automatically opened after my search, so to have quickly the detailed records. Could you help me if possible (I guess with a Post-Search Script)? Thanks very much!

Parvares commented 1 year ago

Hi Mike, may I ask you another related question (I don't know if feasible)? Is it possible to aggregate multiple tabs (for example 15 or more search results) on one long page?

Thank you very much!

P.S. I tried this addon, but doesn't work for those links... Merge all tabs for print or save

ssborbis commented 1 year ago

It's likely possible, but not exactly simple. I'm not familiar with tampermonkey enough to say for sure.

Parvares commented 1 year ago

Okay, never mind if it's hard, thanks anyway!

Parvares commented 1 year ago

Hi, Mike, may a ask a little variation of the above script (post Dec 11, 2022), if you can help me? The iframes of that script display the results of "Lo trovi in" > "Biblioteca Morante" tab, if one exists (post Nov 27, 2022). Is it possible to display instead for every record and every library (not only Biblioteca Morante) how many times there is thie string 'Rientro previsto'? For example in this case a record should simply display: Occurrences: 3

I tried this but the results are not consistent and may vary even starting from the same search. Thanks very much!

(function() {
    'use strict';

    let url = window.location.href;

    const main = async() => {
        await new Promise(r => setTimeout(r, 500));

        if (window != top) return;

        let links = [...document.querySelectorAll(".titololistarisultati a[href][title]")];

        let startIndex = (() => {
            let el = document.querySelector(".list-document > LI");

            if (!el) return -999999;

            let id = el.id;

            if (!id) return -999999;

            let num = id.match(/listadocumenti_(\d+)/)[1];

            return parseInt(num);
        })();

        for (let i = 0; i < links.length; i++) {
            try {
                let link = links[i];
                let index = i + startIndex;

                let id = `#listadocumenti_${index}`;
                let li = document.querySelector(id);

                let container = document.createElement('div');
                container.style = 'margin-left: 16px';
                container.innerText = " [ loading ] ";

                li.appendChild(container);

                let f = document.createElement('iframe');
                f.style.display = 'none';
                document.body.appendChild(f);

                f.onload = async function () {
                    if (f.src === null) return;

                    try {
                        await new Promise(function (r) {
                            setInterval(function () {
                                if (f.contentWindow.document.getElementById("tabDocument_item_tabloca"))
                                    r();
                            }, 100);
                        });

                        f.contentWindow.document.getElementById("tabDocument_item_tabloca").click();

                        await new Promise(function (r) {
                            setTimeout(r, 100);
                        });

                        let occurrences = 0;
                        let elements = f.contentWindow.document.querySelectorAll('div.inventario');
                        elements.forEach(function (element) {
                            if (element.textContent.includes('Rientro previsto')) {
                                occurrences++;
                            }
                        });

                        let infoContainer = document.createElement('div');
                        infoContainer.innerText = "Occurrences: " + occurrences;
                        li.appendChild(infoContainer);
                        container.parentNode.removeChild(container);
                    } catch (error) {
                        container.innerText = " [ failed ] ";
                        console.log(error)
                    }

                    f.src = null;
                };

                f.onerror = function () {
                    container.innerText = " [ failed ] ";
                    f.src = null;
                };

                f.src = link.href;
            } catch (error) {
                console.log(error);
            }

            await new Promise(function (r) {
                setTimeout(r, 100);
            });
        }
    };

    main();

    setInterval(function () {
        if (window.location.href !== url) {
            url = window.location.href;
            main();
        }
    }, 1000);
})();
ssborbis commented 1 year ago

I don't see the phrase Rientro previsto anywhere in the link you provided.

image

Could you explain a bit more and perhaps provide some images to guide me?

ssborbis commented 1 year ago

Ok, after looking a bit more, it seems you'd need your script to "click" each of those locations ( 6 in the image I linked ) to get the page to generate the div.inventario elements. I'm still a bit unsure what you're wanting. Do you want to simply count the number of libraries listed ( 6 in this case )?

ssborbis commented 1 year ago

Maybe something like this:

// ==UserScript==
// @name         bibliotechediroma ( counting )
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://www.bibliotechediroma.it/opac/query/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bibliotechediroma.it
// @grant        none
// ==/UserScript==

// https://www.bibliotechediroma.it/opac/query/stephen%20king?context=tmatm

(function() {
    'use strict';

    let url = window.location.href;

    const main = async() => {
        await new Promise(r => setTimeout(r, 500));

        if (window != top) return;

        let links = [...document.querySelectorAll(".titololistarisultati a[href][title]")];

        let startIndex = (() => {
            let el = document.querySelector(".list-document > LI");

            if (!el) return -999999;

            let id = el.id;

            if (!id) return -999999;

            let num = id.match(/listadocumenti_(\d+)/)[1];

            return parseInt(num);
        })();

        for (let i = 0; i < links.length; i++) {
            try {
                let link = links[i];
                let index = i + startIndex;

                let id = `#listadocumenti_${index}`;
                let li = document.querySelector(id);

                let container = document.createElement('div');
                container.style = 'margin-left: 16px';
                container.innerText = " [ loading ] ";

                li.appendChild(container);

                let f = document.createElement('iframe');
                f.style.display = 'none';
                document.body.appendChild(f);

                f.onload = async function () {
                    if (f.src === null) return;

                    try {
                        await new Promise(function (r) {
                            setInterval(function () {
                                if (f.contentWindow.document.getElementById("tabDocument_item_tabloca"))
                                    r();
                            }, 100);
                        });

                        f.contentWindow.document.getElementById("tabDocument_item_tabloca").click();

                        await new Promise(function (r) {
                            setTimeout(r, 100);
                        });

                        let occurrences = 0;
                       // let elements = f.contentWindow.document.querySelectorAll('div.inventario');
                        let elements = f.contentWindow.document.querySelectorAll('#biblioteche > UL > LI > A');

                        for ( let element of elements ) {

                            element.click();

                            await new Promise(function (r) {
                                setTimeout(r, 50);
                            });

                            let info = f.contentWindow.document.querySelector('div.inventario');
                            if (info && info.textContent.includes('Rientro previsto')) {
                                occurrences++;
                            }
                        }

                        let infoContainer = document.createElement('div');
                        infoContainer.innerText = "Occurrences: " + occurrences;
                        li.appendChild(infoContainer);
                        container.parentNode.removeChild(container);
                    } catch (error) {
                        container.innerText = " [ failed ] ";
                        console.log(error)
                    }

                    f.src = null;
                };

                f.onerror = function () {
                    container.innerText = " [ failed ] ";
                    f.src = null;
                };

                f.src = link.href;
            } catch (error) {
                console.log(error);
            }

            await new Promise(function (r) {
                setTimeout(r, 100);
            });
        }
    };

    main();

    setInterval(function () {
        if (window.location.href !== url) {
            url = window.location.href;
            main();
        }
    }, 1000);
})();

image

Parvares commented 1 year ago

Hi Mike, sorry I didn't get the notification, I only noticed today your reply! :-( Regarding the link, yes i simply want to count the number of libraries listed which have the string "rientro previsto", but this is the path to detect them: "Apri la lista completa (20)" > "Tutte le biblioteche" [all libraries]. That's it, thank again!!