So I have a shortcut that scans some data from a website’s html. Problem is, that website requires a login. Currently, I need to login, then run the shortcut from the share sheet. I already have a script that logs me into the website, but it uses the Scriptable webview to do it. Is there any way for me to get that html data from the webview? If you have any other ideas or help I’m down to hear it.
Skimmer Shortcut: Shortcuts
Here’s the script:
FUNCTIONAL DATE EXTRACTOR
// Function to extract and format date and time from the web page
function extractAndFormatDateTimes() {
// Fetch the date range from the specific <div> element containing <h3>
let dateRangeDiv = document.querySelector('div.pcty-inline-block.pcty-margin-compact-left h3');
let dateRangeHTML = dateRangeDiv ? dateRangeDiv.textContent.trim() : "Date range element not found";
// If the date range element is not found, return the message
if (dateRangeHTML === "Date range element not found") {
return dateRangeHTML;
}
// Parse the start and end dates from the date range
let [startDateStr, endDateStr] = dateRangeHTML.split(' - ').map(date => new Date(date.trim()));
// Validate date parsing
if (isNaN(startDateStr.getTime()) || isNaN(endDateStr.getTime())) {
return "Invalid date range format";
}
// Get the inner text of the <div> with the specified class
let div = document.querySelector('div.pcty-row-flex.pcty-margin-none.pcty-padding-none.css-1vqxman');
let innerText = div ? div.innerText.trim() : "Element not found";
// If element is not found, return the message
if (innerText === "Element not found") {
return innerText;
}
// Split the inner text into lines
let lines = innerText.split('\n').map(line => line.trim());
// Array to store the results
let results = [];
// Loop through the lines to extract dates and times
for (let i = 0; i < lines.length; i++) {
let day = lines[i];
let dayOfWeek = lines[i + 1];
let daycareText = lines[i + 2];
let timeRange = lines[i + 3];
// Check if the line contains "Daycare [DC]" text
if (daycareText === "Daycare [DC]") {
// Remove the (x) part from the time range
timeRange = timeRange.replace(/\(\d+h\)/, '').trim();
// Format the date
let date = new Date(startDateStr);
date.setDate(startDateStr.getDate() + (parseInt(day) - startDateStr.getDate()));
let formattedDate = `${(date.getMonth() + 1).toString().padStart(2, '0')}/${day}/${date.getFullYear()}`;
// Format the entry
let formattedEntry = `Date: ${formattedDate}, Time Range: ${timeRange}`;
// Add the formatted entry to the results array
results.push(formattedEntry);
// Skip the next three lines (day, dayOfWeek, daycareText, timeRange)
i += 3;
}
}
// Join the results into a single text string
return results.length > 0 ? results.join('\n') : "No matching dates and times found";
}
// Execute the function and call the completion handler with the result
completion(extractAndFormatDateTimes());
I think that you can use this script 1:1 in the WebView. Just pass it as string to await wv.evaluateJavaScript(script, true)
. script
is your extraction script and the second argument states that the script should run asynchronous and the completion
function is then available. The return value is your extracted data.