Last active
April 20, 2024 01:14
-
-
Save tyhallcsu/f8fd8a26314aa0a02306a1d9d5492bb8 to your computer and use it in GitHub Desktop.
Verbose Extract Full WikiArt Painting Information [Userscript]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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."); | |
} | |
}); | |
})(); |
Screenshot:
https://imgur.com/OOdK3W6
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
Here was v1 of the script also, for historical reference: