Newer
Older
save-to-mylist / popup.js
@ApricotSavor ApricotSavor on 21 Jul 2016 1 KB 安定版
var mylist;

function setMylist() {
    // マイリスト一覧を取得
    chrome.runtime.sendMessage({
        type: "mylist"
    }, function (response) {
        mylist = response;
        var select = document.getElementById('select');
        mylist.mylistgroup.forEach(function(item) {
            var option = document.createElement('option');
            option.innerHTML = item.name;
            option.value = item.id;
            select.appendChild(option);
        });
        checkButton();
    });
}

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    switch (request.type) {
        case "finish":
            setMylist();
            break;
        case "hasAdded":
            checkButton();
            break;
        default:
            break;
    }
});

function init() {
    chrome.runtime.getBackgroundPage(function(backgroundPage) {
        backgroundPage.init();
    });
}

function checkButton() {
    document.getElementById('toggle').disabled = false;
    chrome.runtime.getBackgroundPage(function(backgroundPage) {
        if(!backgroundPage.isEnabled) {
            document.form.select.disabled = false;
            document.getElementById('toggle').setAttribute("value", "はじめる");
        } else {
            document.form.select.disabled = true;
            document.form.select.value = backgroundPage.getMylist();
            document.getElementById('toggle').setAttribute("value", "おわる");
        }
    });
}

document.getElementById('toggle').onclick = function() {
    chrome.runtime.getBackgroundPage(function(backgroundPage) {
        if(!backgroundPage.isEnabled) {
            backgroundPage.start(document.form.select.value);
            document.getElementById('toggle').disabled = true;
            document.form.select.disabled = true;
        } else {
            backgroundPage.stop(/*"終了しました"*/);
            window.close();
        }
    });
};

window.onload = function () {
    init();
}