久しぶりのソースコード公開。
[ブログで音楽を紹介したい!]iTunesアルバムの視聴を超簡単に貼り付け・埋め込みできるツールを作ったのソースコード。
以下の感じに作っています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
/*! * https://himakan.net/other/journal/sample-music-generator_album-ver * MIT license */ var aag = {}; ( function() { 'use strict'; var appleToolUrl = 'https://tools.applemusic.com/embed/v1/album/', appleToolCountry = '?country=jp', searchUrl = 'https://itunes.apple.com/search', limit = 30, offset = 0, results_ele = $( '#results' ), affiliate_param = '&at=', attribute_artist = 'artistTerm', attribute_album = 'albumTerm', attribute_song = 'songTerm', params = { term: '', media: 'music', entity: 'song', attribute: '', country: 'jp', lang: 'ja_jp', offset: offset, limit: limit }; aag.result_list = []; aag.attribute = ''; function searchArtists() { $.getJSON( searchUrl, params, function( data, status ) { $.each( data.results, function( key, result ) { results_ele.append( build_result( result ) ); aag.result_list.push( result ); }); set_params_offset( offset + limit ); } ) } function set_params_attribute( attribute ) { params.attribute = aag.attribute = attribute } function set_params_offset( value ) { params.offset = offset = value } function reset_result() { clear_result_list(); set_params_term( '' ); reset_params_offset(); remove_results(); } function clear_result_list() { aag.result_list = []; } function set_params_term( value ) { params.term = value } function reset_params_offset() { params.offset = offset = 0 } function remove_results() { results_ele.children().remove(); } function build_result( result ) { return $( '<div>', { 'data-song-no': aag.result_list.length }).append( $( '<img>', { src: result.artworkUrl100 }) ).append( $( '<p>', { text: 'アーティスト名:' + result.artistName }) ).append( $( '<p>', { text: 'アルバム名:' + result.collectionName }) ).append( $( '<p>', { text: '曲名:' + result.trackName }) ).click( function( event ) { set_generator( event.currentTarget ); }) } function set_generator( div_ele ) { var generator = $( '#generator' ), preview = $( '#preview' ), result = aag.result_list[ div_ele.dataset.songNo ], sample = html_generator( result ); generator.children().remove(); generator .append( $( '<textarea>', { id: 'generator_text', text: sample.prop('outerHTML').replace(/&/g,'&') }) ); preview.children().remove(); preview.append( sample ); $( '#copy' ).css( 'display', 'inline' ).text( 'コピー' ); } function html_generator( result ) { return $( '<iframe>',{ src: src_generator( appleToolUrl + result.collectionId + appleToolCountry ), frameborder: 0 }).attr('height', '500px').attr('width', '100%'); } function src_generator( url ) { var token = get_affiliate_token(); $.cookie( 'affiliate_token', token ); if ( '' === token ) { return url } else { return url + affiliate_param + token } } function get_affiliate_token() { var token = $( '#affiliate_token' ).val(); $.cookie( 'affiliate_token', token ); return token } $( '#artist' ).keypress( function( e ) { if ( e.which === 13 ) { search( attribute_artist, e.target.value ); return false; } }); $( '#album' ).keypress( function( e ) { if ( e.which === 13 ) { search( attribute_album, e.target.value ); return false; } }); $( '#song' ).keypress( function( e ) { if ( e.which === 13 ) { search( attribute_song, e.target.value ); return false; } }); function search( attribute, team ) { reset_result(); set_params_attribute( attribute ); set_params_term( team ); $( '#more' ).css( 'display', 'inline' ); searchArtists(); } $( '#more' ).click( function() { set_params_attribute( aag.attribute ); searchArtists(); }); $( '#copy' ).click( function() { $( '#generator_text' ).select(); document.execCommand( 'copy' ); $( '#copy' ).text( 'コピーしました!' ); }); $( '#affiliate_token' ).val( $.cookie( 'affiliate_token' ) ); })(); |
いつにもまして汚いコードになってしまった。。
もっと簡略化できるところとカプセルとかしっかり設計してみたい。
正直メソッド名だけで何しているかわからん。
もっと命名は考えてやらないとだめ。
がんがる。
スポンサードリンク