Greasy Fork is available in English.

AtCoder Listing Tasks

[問題]タブをクリックすると、各問題のページに移動できるドロップダウンリストを表示します。

2023/07/14のページです。最新版はこちら

作者のサイトでサポートを受ける。または、このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
  1. // ==UserScript==
  2. // @name AtCoder Listing Tasks
  3. // @name:en AtCoder Listing Tasks
  4. // @namespace https://github.com/luuguas/AtCoderListingTasks
  5. // @version 1.5.1
  6. // @description [問題]タブをクリックすると、各問題のページに移動できるドロップダウンリストを表示します。
  7. // @description:en Click [Tasks] tab to open a drop-down list linked to each problem.
  8. // @author luuguas
  9. // @license Apache-2.0
  10. // @supportURL https://github.com/luuguas/AtCoderListingTasks/issues
  11. // @match https://atcoder.jp/contests/*
  12. // @exclude https://atcoder.jp/contests/
  13. // @exclude https://atcoder.jp/contests/?*
  14. // @exclude https://atcoder.jp/contests/archive*
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. //AtCoderに標準で読み込まれているjQueryを使用
  22. let $ = window.jQuery;
  23.  
  24. const CONTEST_URL = 'https://atcoder.jp/contests';
  25. const ID_PREFIX = 'userscript-ACLT';
  26. const PRE = 'ACLT';
  27. const CONTEST_REGULAR = ['abc', 'arc', 'agc', 'ahc', 'past', 'joi', 'jag'];
  28. const CONTEST_PERMANENT = ['practice', 'APG4b', 'abs', 'practice2', 'typical90', 'math-and-algorithm', 'tessoku-book'];
  29. const ATONCE_TAB_MAX = 20;
  30.  
  31. const CSS = `
  32. .${PRE}-dropdown {
  33. max-height: 890%;
  34. overflow: visible auto;
  35. }
  36. .${PRE}-label {
  37. width: 100%;
  38. margin: 0px;
  39. padding: 3px 10px;
  40. clear: both;
  41. font-weight: normal;
  42. white-space: nowrap;
  43. }
  44. .${PRE}-label:hover {
  45. background-color: #f5f5f5;
  46. }
  47. .${PRE}-checkbox {
  48. margin: 0px !important;
  49. vertical-align: middle;
  50. }
  51.  
  52. .${PRE}-option {
  53. margin: 5px 0px 15px 10px;
  54. }
  55. .${PRE}-flex {
  56. display: flex;
  57. align-items: center;
  58. }
  59. .${PRE}-select-all {
  60. height: 30px;
  61. }
  62. .${PRE}-select-specify {
  63. height: 35px;
  64. }
  65. .${PRE}-radio {
  66. padding-right: 15px;
  67. }
  68. .${PRE}-disabled {
  69. opacity: 0.65;
  70. }
  71. .${PRE}-caution {
  72. margin-left: 15px;
  73. color: red;
  74. }
  75. .${PRE}-toggle {
  76. min-width: 55px;
  77. }
  78. .${PRE}-caret {
  79. margin-left: 5px !important;
  80. }
  81. .${PRE}-list {
  82. max-height: 800%;
  83. overflow: visible auto;
  84. }
  85. .${PRE}-target {
  86. background-color: #e0e0e0;
  87. }
  88. .${PRE}-target:hover {
  89. background-color: #e0e0e0 !important;
  90. }
  91. .${PRE}-between {
  92. padding: 0px 5px;
  93. }
  94. `;
  95. const TEXT = {
  96. newTab: { 'ja': '新しいタブで開く', 'en': 'Open in a new tab' },
  97. taskTable: { 'ja': '問題一覧', 'en': 'Task Table' },
  98. loadingFailed: { 'ja': '(読み込み失敗)', 'en': '(Loading Failed)' },
  99. atOnce: { 'ja': 'まとめて開く', 'en': 'Open at once' },
  100. modalDiscription: { 'ja': '複数の問題をまとめて開きます。', 'en': 'Open several problems at once.' },
  101. cancel: { 'ja': 'キャンセル', 'en': 'Cancel' },
  102. all: { 'ja': 'すべて', 'en': 'All' },
  103. specify: { 'ja': '範囲を指定', 'en': 'Specify the range' },
  104. caution: { 'ja': `※一度に開くことのできるタブは ${ATONCE_TAB_MAX} 個までです。`, 'en': `*Up to ${ATONCE_TAB_MAX} tabs can be open at once.` },
  105. reverse: { 'ja': '逆順で開く', 'en': 'Open in reverse order' },
  106. modalInfo: { 'ja': 'が開かれます。(ポップアップがブロックされた場合は許可してください。)', 'en': 'will open. (If pop-ups are blocked, please allow them.)' },
  107. aTab: { 'ja': '個のタブ', 'en': 'tab ' },
  108. tabs: { 'ja': '個のタブ', 'en': 'tabs ' },
  109. };
  110.  
  111. const DB_NAME = 'UserScript_ACLT_Database';
  112. const DB_VERSION = 1;
  113. const STORE_NAME = { option: 'option', problemList: 'problemList' };
  114. const STORE_INFO = [{ storeName: STORE_NAME.option, keyPath: 'name' }, { storeName: STORE_NAME.problemList, keyPath: 'contestName' }];
  115.  
  116. const REMOVE_INTERVAL = 1 * 60 * 60 * 1000; //1時間
  117. const REMOVE_BASE = 10 * 24 * 60 * 60 * 1000; //10日
  118. const ACCESS_INTERVAL = 5 * 60 * 1000; //5分
  119.  
  120. /*IndexedDBを扱うクラス
  121. https://github.com/luuguas/IndexedDBManager */
  122. let IDBManager = function (databaseName) { this.database = null; this.databaseName = databaseName; };
  123. IDBManager.prototype = {
  124. openDatabase(storeInfos, version) {
  125. return new Promise((resolve, reject) => {
  126. if (this.database !== null) { resolve(null); return; }
  127. if (typeof window.indexedDB === 'undefined') { reject('IndexedDB is not supported.'); return; }
  128. let openRequest = window.indexedDB.open(this.databaseName, version);
  129. openRequest.onupgradeneeded = (event) => {
  130. let database = event.target.result;
  131. let m = new Map();
  132. for (let name of database.objectStoreNames) m.set(name, { status: 1, keyPath: null });
  133. for (let info of storeInfos) {
  134. if (m.get(info.storeName)) m.set(info.storeName, { status: 2, keyPath: info.keyPath });
  135. else m.set(info.storeName, { status: 0, keyPath: info.keyPath });
  136. }
  137. for (let [name, info] of m) {
  138. if (info.status === 0) database.createObjectStore(name, { keyPath: info.keyPath });
  139. else if (info.status === 1) database.deleteObjectStore(name);
  140. }
  141. console.info('Database was created or upgraded.');
  142. };
  143. openRequest.onerror = (event) => { this.database = null; reject(`Failed to get database. (${event.target.error})`); };
  144. openRequest.onsuccess = (event) => { this.database = event.target.result; resolve(null); };
  145. });
  146. },
  147. isOpened() { return this.database !== null; },
  148. getData(storeName, key) {
  149. return new Promise((resolve, reject) => {
  150. if (!this.isOpened()) { reject('Database is not loaded.'); return; }
  151. let trans = this.database.transaction(storeName, 'readonly');
  152. let getRequest = trans.objectStore(storeName).get(key);
  153. getRequest.onerror = (event) => { reject(`Failed to get data. (${event.target.error})`); };
  154. getRequest.onsuccess = (event) => {
  155. if (event.target.result) resolve(event.target.result);
  156. else resolve(null);
  157. };
  158. });
  159. },
  160. getAllMatchedData(storeName, filter) {
  161. return new Promise((resolve, reject) => {
  162. if (!this.isOpened()) { reject('Database is not loaded.'); return; }
  163. let trans = this.database.transaction(storeName, 'readonly');
  164. let cursorRequest = trans.objectStore(storeName).openCursor();
  165. let res = [];
  166. cursorRequest.onerror = (event) => { reject(`Failed to get cursor. (${event.target.error})`); };
  167. cursorRequest.onsuccess = (event) => {
  168. let cursor = event.target.result;
  169. if (cursor) {
  170. if (filter(cursor.value)) res.push(cursor.value);
  171. cursor.continue();
  172. }
  173. else resolve(res);
  174. };
  175. });
  176. },
  177. setData(storeName, data) {
  178. return new Promise((resolve, reject) => {
  179. if (!this.isOpened()) { reject('Database is not loaded.'); return; }
  180. let trans = this.database.transaction(storeName, 'readwrite');
  181. let setRequest = trans.objectStore(storeName).put(data);
  182. setRequest.onerror = (event) => { reject(`Failed to set data. (${event.target.error})`); };
  183. setRequest.onsuccess = (event) => { resolve(null); };
  184. });
  185. },
  186. deleteData(storeName, key) {
  187. return new Promise((resolve, reject) => {
  188. if (!this.isOpened()) { reject('Database is not loaded.'); return; }
  189. let trans = this.database.transaction(storeName, 'readwrite');
  190. let deleteRequest = trans.objectStore(storeName).delete(key);
  191. deleteRequest.onerror = (event) => { reject(`Failed to delete data. (${event.target.error})`); };
  192. deleteRequest.onsuccess = (event) => { resolve(null); };
  193. });
  194. },
  195. };
  196.  
  197. /* 設定や問題リストの読み込み・保存をするクラス */
  198. let Setting = function () {
  199. this.problemList = null;
  200. this.newTab = null;
  201. this.lastRemove = null;
  202. this.reverse = null;
  203. this.atOnceSetting = null;
  204. this.atOnce = {
  205. begin: 0,
  206. end: 0,
  207. };
  208. this.lang = null;
  209. this.contestName = null;
  210. this.contestCategory = null;
  211. this.db = new IDBManager(DB_NAME);
  212. this.dbExists = false;
  213. };
  214. Setting.prototype = {
  215. openDB: async function () {
  216. try {
  217. await this.db.openDatabase(STORE_INFO, DB_VERSION);
  218. }
  219. catch (err) {
  220. console.warn('[AtCoder Listing Tasks] ' + err);
  221. }
  222. this.dbExists = this.db.isOpened();
  223. },
  224. requestList: function (contestName) {
  225. return new Promise((resolve, reject) => {
  226. let xhr = new XMLHttpRequest();
  227. xhr.responseType = 'document';
  228. xhr.onreadystatechange = function () {
  229. if (xhr.readyState === 4) {
  230. if (xhr.status === 200) {
  231. let result = $(xhr.responseXML);
  232. let problem_node = result.find('#contest-nav-tabs + .col-sm-12');
  233. let problem_list = problem_node.find('tbody tr');
  234. //問題リストを抽出して配列に格納
  235. let list = [];
  236. problem_list.each((idx, val) => {
  237. let td = $(val).children('td');
  238. list.push({
  239. url: td[0].firstChild.getAttribute('href'),
  240. diff: td[0].firstChild.textContent,
  241. name: td[1].firstChild.textContent,
  242. });
  243. });
  244. resolve(list);
  245. }
  246. else {
  247. resolve(null);
  248. }
  249. }
  250. };
  251. //https://atcoder.jp/contests/***/tasksのページ情報をリクエスト
  252. xhr.open('GET', `${CONTEST_URL}/${contestName}/tasks`, true);
  253. xhr.send(null);
  254. });
  255. },
  256. loadData: async function () {
  257. if (this.dbExists) {
  258. //データベースから情報を読み込む
  259. let resArray = await Promise.all([
  260. this.db.getData(STORE_NAME.problemList, this.contestName),
  261. this.db.getData(STORE_NAME.option, 'newTab'),
  262. this.db.getData(STORE_NAME.option, 'lastRemove'),
  263. this.db.getData(STORE_NAME.option, 'atOnce'),
  264. this.db.getData(STORE_NAME.option, 'reverse')
  265. ]);
  266. let setTasks = [];
  267. let now = Date.now();
  268. //設定を格納
  269. if (resArray[1] !== null) {
  270. this.newTab = resArray[1].value;
  271. }
  272. else {
  273. this.newTab = false;
  274. setTasks.push(this.db.setData(STORE_NAME.option, { name: 'newTab', value: this.newTab }));
  275. }
  276. if (resArray[2] !== null) {
  277. this.lastRemove = resArray[2].value;
  278. }
  279. else {
  280. this.lastRemove = now;
  281. setTasks.push(this.db.setData(STORE_NAME.option, { name: 'lastRemove', value: this.lastRemove }));
  282. }
  283. if (resArray[3] !== null) {
  284. this.atOnceSetting = resArray[3].value;
  285. }
  286. else {
  287. this.atOnceSetting = {};
  288. setTasks.push(this.db.setData(STORE_NAME.option, { name: 'atOnce', value: {} }));
  289. }
  290. if (resArray[4] !== null) {
  291. this.reverse = resArray[4].value;
  292. }
  293. else {
  294. this.reverse = false;
  295. setTasks.push(this.db.setData(STORE_NAME.option, { name: 'reverse', value: false }));
  296. }
  297. //問題リストを格納
  298. if (resArray[0] !== null) {
  299. this.problemList = resArray[0].list;
  300. //lastAccessが現在時刻からACCESS_INTERVAL以上前なら更新する
  301. if (now - resArray[0].lastAccess >= ACCESS_INTERVAL) {
  302. setTasks.push(this.db.setData(STORE_NAME.problemList, { contestName: this.contestName, list: this.problemList, lastAccess: now }));
  303. }
  304. }
  305. else {
  306. this.problemList = await this.requestList(this.contestName);
  307. if (this.problemList !== null) {
  308. setTasks.push(this.db.setData(STORE_NAME.problemList, { contestName: this.contestName, list: this.problemList, lastAccess: now }));
  309. }
  310. }
  311. //情報を更新
  312. await Promise.all(setTasks);
  313. }
  314. else {
  315. this.problemList = await this.requestList(this.contestName);
  316. this.newTab = false;
  317. this.lastRemove = null;
  318. this.atOnceSetting = {};
  319. this.reverse = false;
  320. }
  321. },
  322. saveData: async function (name, value) {
  323. if (!this.dbExists) {
  324. return;
  325. }
  326. await this.db.setData(STORE_NAME.option, { name, value });
  327. },
  328. removeOldData: async function () {
  329. if (!this.dbExists) {
  330. return;
  331. }
  332. let now = Date.now();
  333. if (now - this.lastRemove < REMOVE_INTERVAL) {
  334. return;
  335. }
  336. //最終アクセスが現在時刻より一定以上前の問題リストを削除する
  337. let oldData = await this.db.getAllMatchedData(STORE_NAME.problemList, (data) => { return now - data.lastAccess >= REMOVE_BASE; });
  338. if (oldData.length !== 0) {
  339. let deleteTasks = [];
  340. for (let data of oldData) {
  341. deleteTasks.push(this.db.deleteData(STORE_NAME.problemList, data.contestName));
  342. }
  343. await Promise.all(deleteTasks);
  344. }
  345. //lastRemoveを更新する
  346. this.lastRemove = now;
  347. await this.db.setData(STORE_NAME.option, { name: 'lastRemove', value: this.lastRemove });
  348. },
  349. getLanguage: function () {
  350. this.lang = 'ja';
  351. let content_language = $('meta[http-equiv="Content-Language"]');
  352. if (content_language.length !== 0 && content_language.attr('content') === 'en') {
  353. this.lang = 'en';
  354. }
  355. },
  356. getContestName: function () {
  357. this.contestName = window.location.href.split('/')[4];
  358. //ハッシュ(#?)があれば取り除く
  359. let hash = this.contestName.search(/[#\?]/);
  360. if (hash !== -1) {
  361. this.contestName = this.contestName.slice(0, hash);
  362. }
  363. },
  364. getContestCategoryAndAtOnce: function () {
  365. if (this.problemList === null) {
  366. return;
  367. }
  368. //コンテストの種類を取得
  369. let got = false;
  370. if (!got) {
  371. for (let category of CONTEST_REGULAR) {
  372. if (this.contestName.startsWith(category)) {
  373. this.contestCategory = category + '-' + (this.problemList.length).toString();
  374. got = true;
  375. break;
  376. }
  377. }
  378. }
  379. if (!got) {
  380. for (let category of CONTEST_PERMANENT) {
  381. if (this.contestName.startsWith(category)) {
  382. this.contestCategory = category;
  383. got = true;
  384. break;
  385. }
  386. }
  387. }
  388. if (!got) {
  389. this.contestCategory = 'other';
  390. }
  391. //atOnceの設定
  392. if (this.atOnceSetting[this.contestCategory]) {
  393. this.atOnce = this.atOnceSetting[this.contestCategory];
  394. }
  395. else {
  396. this.atOnce.begin = 0;
  397. this.atOnce.end = Math.min(ATONCE_TAB_MAX - 1, this.problemList.length - 1);
  398. }
  399. },
  400. };
  401.  
  402. /* DOM操作およびスクリプト全体の動作を管理するクラス */
  403. let Launcher = function () {
  404. this.setting = new Setting();
  405. this.dropdownList = {
  406. begin: null,
  407. end: null,
  408. };
  409. this.listChanged = {
  410. begin: true,
  411. end: true,
  412. };
  413. this.isAll = true;
  414. };
  415. Launcher.prototype = {
  416. loadSetting: async function () {
  417. this.setting.getContestName();
  418. await this.setting.openDB();
  419. await this.setting.loadData();
  420. this.setting.getLanguage();
  421. this.setting.getContestCategoryAndAtOnce();
  422. },
  423. attachId: function () {
  424. let tabs = $('#contest-nav-tabs');
  425. if (tabs.length === 0) {
  426. return false;
  427. }
  428. let tasks_tab = tabs.find('a[href$="tasks"]');
  429. if (tasks_tab.length === 0) {
  430. return false;
  431. }
  432. else {
  433. tasks_tab.attr('id', `${ID_PREFIX}-tab`);
  434. return true;
  435. }
  436. },
  437. addCss: function () {
  438. let style = $('<style>', { id: `${ID_PREFIX}-style`, html: CSS });
  439. $('head').append(style);
  440. },
  441. changeToDropdown: function () {
  442. let tasks_tab = $(`#${ID_PREFIX}-tab`);
  443. tasks_tab.attr({
  444. 'class': 'dropdown-toggle',
  445. 'data-toggle': 'dropdown',
  446. 'href': '#',
  447. 'role': 'button',
  448. 'aria-haspopup': 'true',
  449. 'aria-expanded': 'false',
  450. });
  451. tasks_tab.append($('<span>', { class: 'caret' }));
  452. tasks_tab.parent().append($('<ul>', { class: `dropdown-menu ${PRE}-dropdown` }));
  453. },
  454. addList: function () {
  455. let dropdown_menu = $(`#${ID_PREFIX}-tab`).parent().children('.dropdown-menu');
  456. /* [問題一覧]の追加 */
  457. let task_table = $('<a>', { href: `${CONTEST_URL}/${this.setting.contestName}/tasks` });
  458. task_table.append($('<span>', { class: 'glyphicon glyphicon-list', 'aria-hidden': 'true' }));
  459. task_table.append(document.createTextNode(' ' + TEXT.taskTable[this.setting.lang]));
  460. //チェックボックスにチェックが付いていたら新しいタブで開く
  461. task_table[0].addEventListener('click', { handleEvent: this.changeNewTabAttr, setting: this.setting });
  462. dropdown_menu.append($('<li>').append(task_table));
  463. /* [まとめて開く]の追加 */
  464. if (this.setting.problemList !== null) {
  465. let at_once = $('<a>');
  466. at_once.append($('<span>', { class: 'glyphicon glyphicon-sort-by-attributes-alt', 'aria-hidden': 'true' }));
  467. at_once.append(document.createTextNode(' ' + TEXT.atOnce[this.setting.lang] + '...'));
  468. at_once.on('click', (e) => {
  469. $(`#${ID_PREFIX}-modal`).modal('show');
  470. });
  471. dropdown_menu.append($('<li>').append(at_once));
  472. }
  473. /* [新しいタブで開く]の追加 */
  474. let label = $('<label>', { class: `${PRE}-label` });
  475. label.css('color', task_table.css('color')); //[問題一覧]から色情報を取得
  476. let checkbox = $('<input>', { type: 'checkbox', class: `${PRE}-checkbox` });
  477. //チェックボックスはチェック状態をストレージと同期
  478. checkbox.prop('checked', this.setting.newTab);
  479. checkbox.on('click', (e) => {
  480. this.setting.newTab = e.currentTarget.checked;
  481. if (this.setting.dbExists) {
  482. this.setting.saveData('newTab', this.setting.newTab);
  483. }
  484. });
  485. label.append(checkbox);
  486. label.append(document.createTextNode(' ' + TEXT.newTab[this.setting.lang]));
  487. dropdown_menu.prepend($('<li>').append(label));
  488. //チェックボックスが押された場合はドロップダウンリストを非表示にしない
  489. dropdown_menu.on('click', (e) => {
  490. if (e.target === label[0]) {
  491. e.stopPropagation();
  492. }
  493. });
  494. /* 分割線の追加 */
  495. dropdown_menu.append($('<li>', { class: 'divider' }));
  496. /* 各問題の追加 */
  497. if (this.setting.problemList !== null) {
  498. //リストを追加
  499. for (let data of this.setting.problemList) {
  500. let a = $('<a>', { href: data.url, text: `${data.diff} - ${data.name}` });
  501. //チェックボックスにチェックが付いていたら新しいタブで開く
  502. a[0].addEventListener('click', { handleEvent: this.changeNewTabAttr, setting: this.setting });
  503. dropdown_menu.append($('<li>').append(a));
  504. }
  505. }
  506. else {
  507. //エラー情報を追加
  508. let a = $('<a>', { text: TEXT.loadingFailed[this.setting.lang] });
  509. dropdown_menu.append($('<li>').append(a));
  510. }
  511. },
  512. changeNewTabAttr: function (e) {
  513. let a = e.currentTarget;
  514. if (this.setting.newTab) {
  515. a.target = '_blank';
  516. a.rel = 'noopener noreferrer';
  517. }
  518. else {
  519. a.target = '_self';
  520. a.rel = '';
  521. }
  522. },
  523. addModal: function () {
  524. if (this.setting.problemList === null) {
  525. return;
  526. }
  527. let modal = $('<div>', { id: `${ID_PREFIX}-modal`, class: 'modal fade', tabindex: '-1', role: 'dialog' });
  528. /* header */
  529. let header = $('<div>', { class: 'modal-header' });
  530. let x = $('<button>', { type: 'button', class: 'close', 'data-dismiss': 'modal', 'aria-label': 'Close' });
  531. x.append($('<span>', { 'aria-hidden': true, text: '×' }));
  532. header.append(x);
  533. header.append($('<h4>', { class: 'modal-title', text: TEXT.atOnce[this.setting.lang] }));
  534. /* body */
  535. let body = $('<div>', { class: 'modal-body' });
  536. body.append($('<p>', { text: TEXT.modalDiscription[this.setting.lang] }));
  537. let modalInfo = $('<p>');
  538. let option = $('<div>', { class: `${PRE}-option` });
  539. //ラジオボタン
  540. let all = $('<div>', { class: `${PRE}-flex ${PRE}-select-all` });
  541. let specify = $('<div>', { class: `${PRE}-flex ${PRE}-select-specify` });
  542. let label_all = $('<label>', { class: `${PRE}-label-radio` });
  543. let radio_all = $('<input>', { type: 'radio', name: 'open-type' });
  544. let label_specify = label_all.clone(true);
  545. let radio_specify = radio_all.clone(true);
  546. if (this.setting.atOnce.begin === 0 && this.setting.atOnce.end === this.setting.problemList.length - 1) {
  547. this.isAll = true;
  548. }
  549. else {
  550. this.isAll = false;
  551. }
  552. radio_all.prop('checked', this.isAll);
  553. radio_specify.prop('checked', !this.isAll);
  554. label_all.append(radio_all, document.createTextNode(TEXT.all[this.setting.lang]));
  555. label_specify.append(radio_specify, document.createTextNode(TEXT.specify[this.setting.lang] + ':'));
  556. let caution = $('<span>', { class: `${PRE}-caution` });
  557. if (this.setting.problemList.length > ATONCE_TAB_MAX) {
  558. radio_all.prop('disabled', true);
  559. label_all.addClass(`${PRE}-disabled`);
  560. caution.text(TEXT.caution[this.setting.lang]);
  561. }
  562. all.append($('<div>', { class: `radio ${PRE}-radio` }).append(label_all, caution));
  563. specify.append($('<div>', { class: `radio ${PRE}-radio` }).append(label_specify));
  564. //[範囲を選択]用のドロップダウン
  565. let select_begin = $('<div>', { class: `btn-group` });
  566. let begin_button = $('<button>', { class: `btn btn-default dropdown-toggle ${PRE}-toggle`, 'data-toggle': 'dropdown', 'aria-expanded': 'false', text: 'A', disabled: this.isAll });
  567. begin_button.append($('<span>', { class: `caret ${PRE}-caret` }));
  568. let begin_list = $('<ul>', { class: `dropdown-menu ${PRE}-list` });
  569. $.each(this.setting.problemList, (idx, data) => {
  570. begin_list.append($('<li>').append($('<a>', { text: `${data.diff} - ${data.name}`, 'data-index': (idx).toString() })));
  571. });
  572. select_begin.append(begin_button, begin_list);
  573. let select_end = select_begin.clone(true);
  574. let end_list = select_end.find('ul');
  575. let end_button = select_end.find('button');
  576. let between = $('<span>', { text: '−', class: `${PRE}-between` });
  577. //初期表示の設定
  578. begin_button.html(`${this.setting.problemList[this.setting.atOnce.begin].diff}<span class="caret ${PRE}-caret"></span>`);
  579. end_button.html(`${this.setting.problemList[this.setting.atOnce.end].diff}<span class="caret ${PRE}-caret"></span>`);
  580. this.dropdownList.begin = begin_list.find('a');
  581. this.dropdownList.end = end_list.find('a');
  582. this.dropdownList.begin.eq(this.setting.atOnce.begin).addClass(`${PRE}-target`);
  583. this.dropdownList.end.eq(this.setting.atOnce.end).addClass(`${PRE}-target`);
  584. this.setModalInfo(modalInfo, this.setting, this.isAll);
  585. //ラジオボタンを切り替えたときの動作
  586. radio_all.on('change', (e) => {
  587. this.isAll = true;
  588. begin_button.prop('disabled', true);
  589. end_button.prop('disabled', true);
  590. between.addClass(`${PRE}-disabled`);
  591. this.setModalInfo(modalInfo, this.setting, this.isAll);
  592. });
  593. radio_specify.on('change', (e) => {
  594. this.isAll = false;
  595. begin_button.prop('disabled', false);
  596. end_button.prop('disabled', false);
  597. between.removeClass(`${PRE}-disabled`);
  598. this.setModalInfo(modalInfo, this.setting, this.isAll);
  599. });
  600. //リストを開いたときの動作
  601. select_begin.on('shown.bs.dropdown', (e) => {
  602. if (this.listChanged.begin) {
  603. begin_list.scrollTop(26 * (this.setting.atOnce.begin - 2));
  604. this.listChanged.begin = false;
  605. }
  606. });
  607. select_end.on('shown.bs.dropdown', (e) => {
  608. if (this.listChanged.end) {
  609. end_list.scrollTop(26 * (this.setting.atOnce.end - 2));
  610. this.listChanged.end = false;
  611. }
  612. });
  613. //リストで選択したときの動作
  614. begin_list[0].addEventListener('click', { handleEvent: this.changeRange, that: this, begin_button, end_button, modalInfo, isBegin: true });
  615. end_list[0].addEventListener('click', { handleEvent: this.changeRange, that: this, begin_button, end_button, modalInfo, isBegin: false });
  616. specify.append(select_begin, between, select_end);
  617. //[逆順で開く]チェックボックス
  618. let reverse = $('<div>', { class: 'checkbox' });
  619. let label_reverse = $('<label>');
  620. let check_reverse = $('<input>', { type: 'checkbox', name: 'reverse' });
  621. check_reverse.prop('checked', this.setting.reverse);
  622. check_reverse.on('click', (e) => {
  623. this.setting.reverse = e.currentTarget.checked;
  624. });
  625. label_reverse.append(check_reverse, document.createTextNode(TEXT.reverse[this.setting.lang]));
  626. reverse.append(label_reverse);
  627. //組み立て
  628. option.append(all, specify, reverse);
  629. body.append(option);
  630. body.append(modalInfo);
  631. /* footer */
  632. let footer = $('<div>', { class: 'modal-footer' });
  633. let cancel = $('<button>', { type: 'button', class: 'btn btn-default', 'data-dismiss': 'modal', text: TEXT.cancel[this.setting.lang] });
  634. let open = $('<button>', { type: 'button', class: 'btn btn-primary', text: TEXT.atOnce[this.setting.lang] });
  635. open.on('click', (e) => {
  636. //設定を保存
  637. this.setting.saveData('reverse', this.setting.reverse);
  638. if (this.setting.contestCategory !== 'other') {
  639. this.setting.atOnceSetting[this.setting.contestCategory] = {};
  640. if (this.isAll) {
  641. this.setting.atOnceSetting[this.setting.contestCategory].begin = 0;
  642. this.setting.atOnceSetting[this.setting.contestCategory].end = this.setting.problemList.length - 1;
  643. }
  644. else {
  645. this.setting.atOnceSetting[this.setting.contestCategory] = this.setting.atOnce;
  646. }
  647. this.setting.saveData('atOnce', this.setting.atOnceSetting);
  648. }
  649. //タブを開く
  650. let blank = window.open('about:blank'); //ポップアップブロック用
  651. let idx = null;
  652. if (this.isAll) {
  653. if (!this.setting.reverse) {
  654. idx = 0;
  655. while (idx <= this.setting.problemList.length - 1) {
  656. window.open(this.setting.problemList[idx].url, '_blank', 'noopener, noreferrer');
  657. ++idx;
  658. }
  659. }
  660. else {
  661. idx = this.setting.problemList.length - 1;
  662. while (idx >= 0) {
  663. window.open(this.setting.problemList[idx].url, '_blank', 'noopener, noreferrer');
  664. --idx;
  665. }
  666. }
  667. }
  668. else {
  669. if (!this.setting.reverse) {
  670. idx = this.setting.atOnce.begin;
  671. while (idx <= this.setting.atOnce.end) {
  672. window.open(this.setting.problemList[idx].url, '_blank', 'noopener, noreferrer');
  673. ++idx;
  674. }
  675. }
  676. else {
  677. idx = this.setting.atOnce.end;
  678. while (idx >= this.setting.atOnce.begin) {
  679. window.open(this.setting.problemList[idx].url, '_blank', 'noopener, noreferrer');
  680. --idx;
  681. }
  682. }
  683. }
  684. modal.modal('hide');
  685. blank.close();
  686. });
  687. footer.append(cancel, open);
  688. /* モーダルウィンドウを追加 */
  689. let dialog = $('<div>', { class: 'modal-dialog', role: 'document' });
  690. let content = $('<div>', { class: 'modal-content' });
  691. content.append(header, body, footer);
  692. modal.append(dialog.append(content));
  693. $('#main-div').before(modal);
  694. },
  695. changeRange: function (e) {
  696. if (e.target.tagName !== 'A') {
  697. return;
  698. }
  699. let atOnce = this.that.setting.atOnce;
  700. let idx = Number($(e.target).attr('data-index'));
  701. if (this.isBegin) {
  702. this.that.changeSelect(this.that, this.begin_button, idx, true);
  703. if (atOnce.end < atOnce.begin) {
  704. this.that.changeSelect(this.that, this.end_button, idx, false);
  705. }
  706. else if (atOnce.end >= atOnce.begin + ATONCE_TAB_MAX) {
  707. this.that.changeSelect(this.that, this.end_button, idx + ATONCE_TAB_MAX - 1, false);
  708. }
  709. }
  710. else {
  711. this.that.changeSelect(this.that, this.end_button, idx, false);
  712. if (atOnce.begin > atOnce.end) {
  713. this.that.changeSelect(this.that, this.begin_button, idx, true);
  714. }
  715. if (atOnce.begin <= atOnce.end - ATONCE_TAB_MAX) {
  716. this.that.changeSelect(this.that, this.begin_button, idx - ATONCE_TAB_MAX + 1, true);
  717. }
  718. }
  719. this.that.setModalInfo(this.modalInfo, this.that.setting, this.that.isAll);
  720. },
  721. changeSelect: function (that, button, idx, isBegin) {
  722. let problemList = that.setting.problemList;
  723. let atOnce = that.setting.atOnce;
  724. let dropdownList = that.dropdownList;
  725. if (isBegin) {
  726. dropdownList.begin.eq(atOnce.begin).removeClass(`${PRE}-target`);
  727. atOnce.begin = idx;
  728. dropdownList.begin.eq(idx).addClass(`${PRE}-target`);
  729. that.listChanged.begin = true;
  730. }
  731. else {
  732. dropdownList.end.eq(atOnce.end).removeClass(`${PRE}-target`);
  733. atOnce.end = idx;
  734. dropdownList.end.eq(idx).addClass(`${PRE}-target`);
  735. that.listChanged.end = true;
  736. }
  737. button.html(`${problemList[idx].diff}<span class="caret ${PRE}-caret"></span>`);
  738. },
  739. setModalInfo: function (modalInfo, setting, isAll) {
  740. let text = '';
  741. if (isAll) {
  742. text += (setting.problemList.length).toString();
  743. text += ' ';
  744. if (setting.problemList.length === 1) {
  745. text += TEXT.aTab[setting.lang];
  746. }
  747. else {
  748. text += TEXT.tabs[setting.lang];
  749. }
  750. }
  751. else {
  752. text += (setting.atOnce.end - setting.atOnce.begin + 1).toString();
  753. text += ' ';
  754. if (setting.atOnce.end === setting.atOnce.begin) {
  755. text += TEXT.aTab[setting.lang];
  756. }
  757. else {
  758. text += TEXT.tabs[setting.lang];
  759. }
  760. }
  761. text += TEXT.modalInfo[setting.lang];
  762. modalInfo.text(text);
  763. },
  764. launch: async function () {
  765. let tabExists = this.attachId();
  766. //[問題]タブがない場合は終了
  767. if (!tabExists) {
  768. console.log('[AtCoder Listing Tasks] [Tasks] Tab isn\'t exist.');
  769. return;
  770. }
  771. await this.loadSetting();
  772. this.addCss();
  773. this.changeToDropdown();
  774. this.addList();
  775. this.addModal();
  776. await this.setting.removeOldData();
  777. if (this.setting.problemList !== null) {
  778. console.log('[AtCoder Listing Tasks] Succeeded!');
  779. }
  780. else {
  781. console.warn('[AtCoder Listing Tasks] Failed...');
  782. }
  783. },
  784. };
  785.  
  786. /* スクリプトを実行 */
  787. let launcher = new Launcher();
  788. launcher.launch();
  789.  
  790. })();