Skip to content

Instantly share code, notes, and snippets.

@tyhallcsu
Last active April 20, 2024 01:14
Show Gist options
  • Save tyhallcsu/f8fd8a26314aa0a02306a1d9d5492bb8 to your computer and use it in GitHub Desktop.
Save tyhallcsu/f8fd8a26314aa0a02306a1d9d5492bb8 to your computer and use it in GitHub Desktop.
Verbose Extract Full WikiArt Painting Information [Userscript]
// ==UserScript==
// @name Verbose Extract Full WikiArt Painting Information
// @namespace http://tampermonkey.net/
// @version 2.0
// @description Correctly extracts full painting information from WikiArt pages with verbose logging for debugging purposes
// @author sharmanhall
// @match https://www.wikiart.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=wikiart.org
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Wait until the page is fully loaded
window.addEventListener('load', function() {
console.log("Page loaded. Starting to search for the painting information...");
// Attempt to find the div that contains the painting JSON information
var paintingInfoDiv = document.querySelector("div[ng-init^='paintingJson']");
if (paintingInfoDiv) {
console.log("Found div with painting information.");
// Extract the ng-init attribute value which contains the JSON string
var ngInitContent = paintingInfoDiv.getAttribute("ng-init");
console.log("ng-init content:", ngInitContent);
// Adjusting the regex to capture the JSON string correctly, accounting for different endings.
var jsonStringMatch = /paintingJson = (\{.*?\})\s*(;|$)/.exec(ngInitContent);
if (jsonStringMatch && jsonStringMatch.length > 1) {
console.log("Extracted JSON string:", jsonStringMatch[1]);
// Parse the JSON string into an object
var paintingInfo;
try {
paintingInfo = JSON.parse(jsonStringMatch[1]);
console.log("Parsed painting information:", paintingInfo);
// Convert all the key-value pairs in the paintingInfo object into a string
var infoString = Object.entries(paintingInfo).map(([key, value]) => `${key}: ${value}`).join('\n');
// Display the extracted information
console.log("Full painting information:", infoString);
alert("Painting information extracted! Check the console for all details.");
alert(infoString);
} catch(e) {
console.error("Error parsing JSON:", e);
alert("There was an error parsing the painting information. Check the console for more details.");
}
} else {
// If the JSON string couldn't be extracted, log and alert the user
console.error("Could not extract JSON from the page's script.");
alert("Could not extract painting information from the page's script. Check the console for more details.");
}
} else {
// If the specific div wasn't found, log and alert the user
console.error("Could not find the painting information container on this page.");
alert("Could not find the painting information container on this page. Check the console for more details.");
}
});
})();
@tyhallcsu
Copy link
Author

tyhallcsu commented Apr 11, 2024

Here was v1 of the script also, for historical reference:

// ==UserScript==
// @name         Extract WikiArt Painting Information
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Extracts painting information from WikiArt pages and displays it as an alert
// @author       sharmanhall
// @match        https://www.wikiart.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wikiart.org
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Wait until the page is fully loaded
    window.addEventListener('load', function() {
        // Attempt to find the div that contains the painting JSON information
        var paintingInfoDiv = document.querySelector("div[ng-init^='paintingJson']");

        // Check if the div was found
        if (paintingInfoDiv) {
            // Extract the ng-init attribute value which contains the JSON string
            var ngInitContent = paintingInfoDiv.getAttribute("ng-init");
            // Extract the JSON string from the ngInitContent
            var jsonString = ngInitContent.match(/paintingJson = ({.*?})/)[1];

            // Check if jsonString was successfully extracted
            if (jsonString) {
                // Parse the JSON string into an object
                var paintingInfo = JSON.parse(jsonString);

                // Construct the message to display
                var message = paintingInfo.title + ' - ' + paintingInfo.year + ' (' + paintingInfo.artistName + ')';

                //var title = paintingInfo.title;
                //var width = paintingInfo.width;
                //var height = paintingInfo.height;
                //var title = paintingInfo.title;
                //var year = paintingInfo.year;
                //var image = paintingInfo.image;
                //var map = paintingInfo.map;
                //var paintingUrl = paintingInfo.paintingUrl;
                //var artistUrl = paintingInfo.artistUrl;
                //var flags = paintingInfo.flags;
                //var albums = paintingInfo.albums;

                // Display the extracted information
                alert(message);
                alert(jsonString);
            } else {
                // If the JSON string couldn't be extracted, alert the user
                alert("Could not find painting information on this page.");
            }
        } else {
            // If the specific div wasn't found, alert the user
            alert("Could not find the painting information container.");
        }
    });
})();

@tyhallcsu
Copy link
Author

@EmeraldBoa
Copy link

Final version is up. While I've no doubt made many mistakes, I feel like I've learned a lot in the process. Thanks again!
https://greasyfork.org/en/scripts/492666-wikiart-downloader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment