• I made a javascript file for browsing wikipedia

    From Morningstarr@HOBBYSPC to ALL on Wednesday, April 29, 2026 18:30:48
    It seems to be failing, I have tried 4 different ways to get it to work. Have a look at the script and see if you guys see anything. Hell, copy and paste it..Try to run it on your board, and see what error you get.

    // Wikipedia Pro for Synchronet BBS
    // Features: Search, Random, History, Caching, and Lightbar Menus
    // Place in /sbbs/exec/wiki.js

    load("http.js");
    load("sbbsdefs.js");

    var CACHE_DIR = system.data_dir + "wiki_cache/";
    if (!file_isdir(CACHE_DIR)) mkdir(CACHE_DIR);

    var summaryMode = false;
    var historyStack = []; // Session-based history

    // --- Utility Functions ---

    function wrapText(str, width) {
    if (!str) return "";
    var regex = new RegExp("(?![^\\n]{1," + width + "}$)([^\\n]{1," + width + "})\\s", "g");
    return str.replace(regex, "$1\r\n");
    }

    function addToHistory(title) {
    var index = historyStack.indexOf(title);
    if (index > -1) historyStack.splice(index, 1);
    historyStack.unshift(title);
    if (historyStack.length > 5) historyStack.pop();
    }

    function safeJSONParse(str) {
    if (!str || typeof str !== "string" || str.trim() === "") return null;
    try {
    return JSON.parse(str);
    } catch (e) {
    return null;
    }
    }

    function getWikipediaData(url, cacheKey) {
    var cacheFile = CACHE_DIR + cacheKey + ".json";

    // Check Cache (24 hour life)
    if (file_exists(cacheFile) && (time() - file_utime(cacheFile) < 86400)) {
    var f = new File(cacheFile);
    if (f.open("r")) {
    var cachedData = safeJSONParse(f.read());
    f.close();
    if (cachedData) return cachedData;
    }
    }

    // Fetch Fresh
    var req = new HTTPRequest();
    var res = req.Get(url);
    var data = safeJSONParse(res);

    if (data) {
    var f = new File(cacheFile);
    if (f.open("w")) {
    f.write(res);
    f.close();
    }
    return data;
    }
    return null;
    }

    // --- Core Logic ---

    function browseWikipedia() {
    while (true) {
    console.clear();
    console.putmsg("\x01h\x01cWikipedia Pro for Synchronet\x01n\r\n"); console.putmsg("\x01h\x01wMode: " + (summaryMode ? "\x01ySummary" : "\x01gFull Article") + "\x01n\r\n\r\n");

    console.putmsg("\x01h\x01g(S)\x01nearch, \x01h\x01g(R)\x01nandom, \x01h\x01g(H)\x01nistories, \x01h\x01g(T)\x01noggle Mode, \x01h\x01g(Q)\x01nuit: ");
    var key = console.getkey(K_UPPER);

    if (key === 'Q') break;
    if (key === 'T') { summaryMode = !summaryMode; continue; }
    if (key === 'R') { fetchRandom(); continue; }
    if (key === 'H') {
    if (historyStack.length === 0) {
    console.putmsg("\r\n\x01h\x01rNo history yet!\x01n");
    console.pause();
    continue;
    }
    var hSelect = uifc.list(WIN_MID | WIN_SAV, "Recent Articles", historyStack);
    if (hSelect >= 0) fetchArticle(historyStack[hSelect]);
    continue;
    }
    if (key === 'S') {
    console.putmsg("\r\n\x01nEnter Search: ");
    var term = console.getstr(60, K_LINE);
    if (term) performSearch(term);
    }
    }
    }

    function performSearch(term) {
    var url = "https://wikipedia.org" + encodeURIComponent(term) + "&limit=10&format=json";
    var data = getWikipediaData(url, "search_" + md5_calc(term));

    // Opensearch returns an array where index 1 is the titles
    if (!data || !data[1] || data[1].length === 0) {
    console.putmsg("\x01h\x01rNo matches found.\x01n");
    console.pause();
    return;
    }

    var selection = uifc.list(WIN_MID | WIN_SAV, "Select Article", data[1]);
    if (selection >= 0) {
    fetchArticle(data[1][selection]);
    }
    }

    function fetchRandom() {
    var url = "https://wikipedia.org";
    var req = new HTTPRequest();
    var res = req.Get(url);
    var data = safeJSONParse(res);

    if (data && data.query && data.query.random && data.query.random[0]) { fetchArticle(data.query.random[0].title);
    } else {
    console.putmsg("\r\n\x01h\x01rError fetching random article.\x01n"); console.pause();
    }
    }

    function fetchArticle(title) {
    addToHistory(title);

    var url = "https://wikipedia.org"
    + encodeURIComponent(title) + (summaryMode ? "&exintro=1" : "") + "&format=json";

    var data = getWikipediaData(url, "art_" + md5_calc(title + summaryMode));
    if (!data || !data.query || !data.query.pages) { console.putmsg("\x01h\x01rArticle not found.\x01n");
    console.pause();
    return;
    }

    var pages = data.query.pages;
    var pageId = Object.keys(pages)[0];
    var extract = pages[pageId].extract;

    if (!extract) {
    console.putmsg("\x01h\x01rThis page is empty or a redirect.\x01n"); console.pause();
    return;
    }

    // Identify potential related topics (Proper Nouns)
    var links = extract.match(/[A-Z][a-z]+(\s[A-Z][a-z]+)*/g) || [];
    links = links.filter(function(v, i, a) {
    return a.indexOf(v) === i && v.length > 4 && v.toUpperCase() !== title.toUpperCase();
    }).slice(0, 10);

    var tempFileName = system.temp_dir + "wiki_tmp.txt";
    var f = new File(tempFileName);
    if (f.open("w")) {
    f.writeln("\x01h\x01y" + title.toUpperCase());
    f.writeln(new Array(title.length + 1).join("="));
    f.writeln("\x01n" + wrapText(extract, 78));
    f.close();

    console.clear();
    console.printfile(tempFileName, P_NOPAUSE);
    file_remove(tempFileName);

    if (links.length > 0) {
    var linkSelect = uifc.list(WIN_MID | WIN_SAV, "Related Topics", links);
    if (linkSelect >= 0) fetchArticle(links[linkSelect]);
    }
    }
    }

    // Start Application
    browseWikipedia();

    ---
    þ Synchronet þ Hobby Space - Express Yourself hobbyspc.synchro.net