diff --git a/background.js b/background.js index 77fde5f..7dd4822 100755 --- a/background.js +++ b/background.js @@ -3,6 +3,7 @@ var isEnabled = false; var dataMylist; var selectMylist; +var selectTags; function setMylist(url) { chrome.tabs.create({"url": url + '?group_id=' + selectMylist, active: false}, function (tab) { @@ -14,6 +15,10 @@ return selectMylist; } +function getTags() { + return selectTags.toString(); +} + function init() { var xhr = new XMLHttpRequest; // 第3引数で非同期を明示的に指定 @@ -43,8 +48,9 @@ isEnabled = false; } -function start(id) { +function start(id, tags) { selectMylist = id; + selectTags = tags.split(','); chrome.tabs.query({currentWindow: true, active: true}, function (tabs) { var tab = tabs[0]; if(!isEnabled) { @@ -54,13 +60,27 @@ isEnabled = true; // マイリス追加 currentMovieUrl = tab.url; - getMylistList(selectMylist, function(list) { - if (list.mylistitem.length < 500) { - chrome.tabs.executeScript(null, {file: "content_script.js"}); - } else { - stop("選択されたマイリストにはこれ以上動画を追加できません"); - } - }); + updateMylist(); + } + }); +} + +function updateMylist() { + getMylistList(selectMylist, function(list) { + if (list.mylistitem.length < 500) { + if (selectTags[0].length) { + getTagsTag(function(tag){ + for (var i in selectTags) { + if (tag == selectTags[i]) { + chrome.tabs.executeScript(null, {file: "content_script.js"}); + } + } + }); + } else { + chrome.tabs.executeScript(null, {file: "content_script.js"}); + } + } else { + stop("選択されたマイリストにはこれ以上動画を追加できません"); } }); } @@ -79,6 +99,19 @@ xhr.send(); } +function getTagsTag(callback) { + chrome.tabs.executeScript(targetTabId, { + code: "document.documentElement.outerHTML" + }, function(result) { + var parser = new DOMParser(); + var dom = parser.parseFromString(result, 'text/html'); + var tags = dom.getElementsByClassName('videoHeaderTagLink'); + for(var i = 0; i < tags.length; i++) { + callback(tags[i].textContent); + } + }); +} + chrome.tabs.onUpdated.addListener(function(tabId, cahngeInfo, tab) { if(tabId == targetTabId && tab.status == 'complete') { @@ -90,7 +123,7 @@ currentMovieUrl = tab.url; // マイリス追加 setTimeout(function() { - chrome.tabs.executeScript(tabId, {file: "content_script.js"}); + updateMylist(); }, 5000) } } else { diff --git a/popup.html b/popup.html index 33ed232..fa1cc4f 100755 --- a/popup.html +++ b/popup.html @@ -9,10 +9,16 @@
自動追加先マイリスト
- -
- +
+ +
+
+ + +
+
+ +
diff --git a/popup.js b/popup.js index 2dd7f28..3beb72c 100755 --- a/popup.js +++ b/popup.js @@ -41,10 +41,13 @@ chrome.runtime.getBackgroundPage(function(backgroundPage) { if(!backgroundPage.isEnabled) { document.form.select.disabled = false; + document.form.tag.disabled = false; document.getElementById('toggle').setAttribute("value", "はじめる"); } else { document.form.select.disabled = true; + document.form.tag.disabled = true; document.form.select.value = backgroundPage.getMylist(); + if (backgroundPage.getTags()) document.form.tag.value = backgroundPage.getTags(); document.getElementById('toggle').setAttribute("value", "おわる"); } }); @@ -53,8 +56,9 @@ document.getElementById('toggle').onclick = function() { chrome.runtime.getBackgroundPage(function(backgroundPage) { if(!backgroundPage.isEnabled) { - backgroundPage.start(document.form.select.value); + backgroundPage.start(document.form.select.value, document.form.tag.value); document.getElementById('toggle').disabled = true; + document.getElementById('tag').disabled = true; document.form.select.disabled = true; } else { backgroundPage.stop(/*"終了しました"*/);