Я использую классы JavaScript, как они должны? - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь создать плагин для Chrome, выполняет некоторые операции по просмотру веб-страниц и показывает информацию. Я довольно новичок в программировании, и я обычно делаю все с функциями, это моя первая попытка с классами. У меня есть этот код, который работает, я просто хочу знать, реализовал ли я его так, как должен. Метод новостей и сделок делает то же самое, что и метод цитат, но с другим источником, поэтому я вырезал его, чтобы не было такого большого количества кода. Что я могу улучшить в этом коде? В коде, когда я использую Plugin.random выглядит странно, это так, как он используется? Я попытался this.random и выдает ошибку.

class Plugin {
    static random(min, max) {
        return Math.random() * (max - min) + min;
    }
    get deals(){
        return this.getDeals();
    }
    get news(){
        return this.getNews();
    }
    get quote(){
        return this.getQuote();
    }
    getDeals() {
        request.open('GET', url, true);
        request.send();
    }
    getNews() {
        request.open('GET', url, true);
        request.send();
    }
    getQuote() {
        for(let i = 10; i > 0; i--) {
            let page = Math.round(Plugin.random(1, 22));
            if (page !== 17) {
                var url = 'https://www.somesite.com?page=' + page;
                break;
            }
        }
        let request = new XMLHttpRequest();
        request.onload = function(event) {
        let parser = new DOMParser();
        let xmlDoc = parser.parseFromString(request.response,"text/html");
        for(let i = 25; i > 0; i--) {
            let nr = Math.round(Plugin.random(1, 24));
           if(xmlDoc.getElementsByClassName("quoteText")[nr].innerHTML.length < 600) {
                var quote = xmlDoc.getElementsByClassName("quoteText")[nr].innerHTML.split('"')[0].split('<br>')[0];
                let title = xmlDoc.getElementsByClassName("quoteText")[nr].getElementsByClassName("authorOrTitle")[0].innerHTML;
              break;
           }
        }
        document.getElementById('beforeand').insertAdjacentHTML('beforeend',quote.substr(8).slice(0, -4) + '<br><p class="author">' + title + '</p>');
        };
        request.open('GET', url, true);
        request.send();
    }
}

const test = new Plugin();
test.quote;
test.news;
test.deals;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...