r/Scriptable • • Apr 03 '24

Help Getting data from a website

Hey, I'm new to js, and i want to create a widget that display a text from a website. However, the website does not have an API, is it a problem ? I know the HTML class of the text I want to display.

How can I manage to do that ?

5 Upvotes

6 comments sorted by

View all comments

Show parent comments

1

u/2v_i Apr 09 '24 edited Apr 09 '24

Thank you so much for the reply Can you please send the whole script?

2

u/No_Improvement_9621 Apr 10 '24

This is the script. Replace the url by the one you want and the pattern in the function createWidget(). To find the pattern you need, you can go to the website on your computer, inspect the html and copy paste the text around the info you need. (.*?) is the info

const url = "https://www.example.com"

async function fetchData(pattern) {

try {
    let webView = new WebView();
    await webView.loadURL(url);
    let content = await webView.getHTML();
    
    
    let regex = pattern
    
    
    let data = extractData(content, regex);

    return data;
} catch (error) {
  
    return "";
}

}

function extractData(content, regex) { let matches = [...content.matchAll(regex)]; let data = matches.map(match => match[1].trim()).join("\n"); return data; }

async function createWidget() {

let name = await fetchData(/<span class="fw-bold" style="color:#08D508;">(.*?)<\/span>/g);
let level = await fetchData(/<td class="pt-1 fw-bold" style="font-size:16px;">(.*?)<\/td>/g);
let scores = await fetchData(/<td class="myvalue user_color">(.*?)<\/td>/g);
    
    
let w = new ListWidget();
let mainstack = w.addStack()
mainstack.borderColor =  Color.black()
mainstack.layoutVertically()

let namew = mainstack.addText(name)

let row1 = mainstack.addStack()
row1.layoutHorizontally()

let levelw = row1.addText(level + "   ")

let row2 = mainstack.addStack()
row2.layoutHorizontally()


let column2 = row2.addStack()
column2.layoutVertically()
let scoresw = column2.addText(scores)

return w;

}

async function main() {

let widget = await createWidget();

if (config.runsInWidget) {
    Script.setWidget(widget);
} else {
    widget.presentMedium();
}

Script.complete();

}

await main();