55// @author James Teh <jamie@jantrid.net>
66// @copyright 2013 James Teh
77// @license GNU General Public License version 2.0
8- // @version 0.20130110 .01
9- // @include http://www.pandora.com/*
8+ // @version 0.20130111 .01
9+ // @include http://www.pandora.com/*
1010// @homepageURL http://userscripts.org/scripts/show/156173
1111// @updateURL https://userscripts.org/scripts/source/156173.user.js
1212// ==/UserScript==
@@ -35,6 +35,19 @@ function fixButton(target) {
3535 }
3636}
3737
38+ function fixStationDetails ( target ) {
39+ document . getElementById ( "addArtistSeed" ) . setAttribute ( "aria-label" , "Add artist" ) ;
40+ var nodes = target . getElementsByClassName ( "deletable" ) ;
41+ for ( var i = 0 ; i < nodes . length ; ++ i ) {
42+ var node = nodes [ i ] ;
43+ node . setAttribute ( "role" , "button" ) ;
44+ node . setAttribute ( "aria-label" , "Delete" ) ;
45+ }
46+ var nodes = target . getElementsByClassName ( "sample" ) ;
47+ for ( var i = 0 ; i < nodes . length ; ++ i )
48+ nodes [ i ] . firstChild . setAttribute ( "aria-label" , "Sample" ) ;
49+ }
50+
3851function onClassModified ( target ) {
3952 var classes = target . classList ;
4053 if ( ! classes )
@@ -46,6 +59,12 @@ function onClassModified(target) {
4659}
4760
4861function onNodeAdded ( target ) {
62+ if ( target . nodeType != Node . ELEMENT_NODE )
63+ return ;
64+ if ( target . classList . contains ( "backstage" ) ) {
65+ fixStationDetails ( target ) ;
66+ return ;
67+ }
4968 var node ;
5069 if ( node = document . getElementById ( "stationList" ) )
5170 node . setAttribute ( "role" , "radiogroup" ) ;
@@ -67,8 +86,16 @@ function onNodeAdded(target) {
6786
6887function onStyleModified ( target ) {
6988 var style = target . style ;
70- if ( target . id == "station_menu_dd" && style . visibility == "visible" )
71- target . getElementsByTagName ( "a" ) [ 0 ] . focus ( ) ;
89+ if ( target . id == "station_menu_dd" && style . visibility == "visible" ) {
90+ var nodes = target . getElementsByTagName ( "a" ) ;
91+ for ( var i = 0 ; i < nodes . length ; ++ i ) {
92+ var node = nodes [ i ] ;
93+ if ( node . style . display == "none" )
94+ continue ;
95+ node . focus ( ) ;
96+ break ;
97+ }
98+ }
7299}
73100
74101function init ( ) {
@@ -81,32 +108,23 @@ function init() {
81108 node . setAttribute ( "role" , "button" ) ;
82109 node . setAttribute ( "aria-label" , "Buy" ) ;
83110 }
84- // Something causes these nodes to remove ARIA attributes if we set them here,
85- // so delay this.
86- setTimeout ( function ( ) {
87- document . getElementById ( "addArtistSeed" ) . setAttribute ( "aria-label" , "Add artist" ) ;
88- var nodes = document . getElementsByClassName ( "deletable" ) ;
89- for ( var i = 0 ; i < nodes . length ; ++ i ) {
90- var node = nodes [ i ] ;
91- node . setAttribute ( "role" , "button" ) ;
92- node . setAttribute ( "aria-label" , "Delete" ) ;
93- }
94- var nodes = document . getElementsByClassName ( "sample" ) ;
95- for ( var i = 0 ; i < nodes . length ; ++ i )
96- nodes [ i ] . firstChild . setAttribute ( "aria-label" , "Sample" ) ;
97- } , 7000 ) ;
98111}
99112
100113var observer = new MutationObserver ( function ( mutations ) {
101114 mutations . forEach ( function ( mutation ) {
102- if ( mutation . type === "childList" ) {
103- for ( var i = 0 ; i < mutation . addedNodes . length ; ++ i )
104- onNodeAdded ( mutation . addedNodes [ i ] ) ;
105- } else if ( mutation . type === "attributes" ) {
106- if ( mutation . attributeName == "class" )
107- onClassModified ( mutation . target ) ;
108- else if ( mutation . attributeName == "style" )
109- onStyleModified ( mutation . target ) ;
115+ try {
116+ if ( mutation . type === "childList" ) {
117+ for ( var i = 0 ; i < mutation . addedNodes . length ; ++ i )
118+ onNodeAdded ( mutation . addedNodes [ i ] ) ;
119+ } else if ( mutation . type === "attributes" ) {
120+ if ( mutation . attributeName == "class" )
121+ onClassModified ( mutation . target ) ;
122+ else if ( mutation . attributeName == "style" )
123+ onStyleModified ( mutation . target ) ;
124+ }
125+ } catch ( e ) {
126+ // Catch exceptions for individual mutations so other mutations are still handled.
127+ GM_log ( "Exception while handling mutation: " + e ) ;
110128 }
111129 } ) ;
112130} ) ;
0 commit comments