Code Monkey home page Code Monkey logo
class encn_KBBI {
    constructor(options) {
        this.options = options;
        this.maxexample = 2;
        this.word = '';
    }

    async displayName() {
        return 'Kamus Besar Bahasa Indonesia';
    }

    setOptions(options) {
        this.options = options;
        this.maxexample = options.maxexample;
    }

    async findTerm(word) {
        this.word = word;
        let results = await Promise.all([this.findKBBI(word)]);
        return [].concat(...results).filter(x => x);
    }

    async findKBBI(word) {
        const maxexample = this.maxexample;
        if (!word) return [];

        let base = 'https://kbbi.kemdikbud.go.id/entri/';
        let url = base + encodeURIComponent(word);
        let doc = '';
        try {
            let data = await api.fetch(url);
            let parser = new DOMParser();
            doc = parser.parseFromString(data, 'text/html');
            let definitions = getKBBIDefinitions(doc);
            return definitions;
        } catch (err) {
            return [];
        }

        function getKBBIDefinitions(doc) {
            let notes = [];

            // Identify the definition section
            let defNodes = doc.querySelectorAll('.container .daftar');

            if (!defNodes || !defNodes.length) return notes;

            // Get headword
            let expression = T(doc.querySelector('.container .lema .h2font'));

            // Prepare definitions
            let definitions = [];
            for (const defNode of defNodes) {
                let definition = T(defNode);
                definitions.push(`<span class="tran">${definition}</span>`);
            }

            let css = `
                <style>
                    span.tran {margin:0; padding:0; color: #0d47a1;}
                </style>`;

            notes.push({
                css,
                expression,
                definitions,
                audios: [] // No audio provided
            });

            return notes;
        }

        function T(node) {
            if (!node) return '';
            else return node.innerText.trim();
        }
    }
}

diana0o0's Projects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.