| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | ( function ( w, d ) { |
| 2 | 2 | function parseJSON( str ) { |
| 3 | 3 | try { |
| 4 | 4 | return str ? w.JSON.parse( str ) : null; |
| 5 | - } catch ( error ) { | |
| 5 | + } catch { | |
| 6 | 6 | return null; |
| 7 | 7 | } |
| 8 | 8 | } |
| 9 | 9 | |
| @@ -23,11 +23,14 @@ | ||
| 23 | 23 | if ( poll ) { |
| 24 | 24 | let poll_url; |
| 25 | 25 | try { |
| 26 | 26 | poll_url = new w.URL( poll.url, 'https://invalid.tld' ); |
| 27 | - } catch ( error ) { | |
| 27 | + } catch { | |
| 28 | 28 | return false; |
| 29 | 29 | } |
| 30 | + if ( poll_url.protocol !== 'https:' ) { | |
| 31 | + return false; | |
| 32 | + } | |
| 30 | 33 | if ( |
| 31 | 34 | poll_url.hostname !== 'secure.polldaddy.com' && |
| 32 | 35 | poll_url.hostname !== 'static.polldaddy.com' |
| 33 | 36 | ) { |
| @@ -33,54 +36,47 @@ | ||
| 33 | 36 | ) { |
| 34 | 37 | return false; |
| 35 | 38 | } |
| 36 | 39 | const pathname = poll_url.pathname; |
| 37 | - if ( ! /\/?p\/\d+\.js/.test( pathname ) ) { | |
| 40 | + if ( ! /^\/p\/\d+\.js$/.test( pathname ) ) { | |
| 38 | 41 | return false; |
| 39 | 42 | } |
| 40 | 43 | const wp_pd_js = d.createElement( 'script' ); |
| 41 | - wp_pd_js.src = poll.url; | |
| 44 | + wp_pd_js.src = poll_url.href; | |
| 42 | 45 | wp_pd_js.async = true; |
| 43 | 46 | d.head.appendChild( wp_pd_js ); |
| 44 | 47 | } |
| 45 | 48 | } ); |
| 46 | 49 | |
| 47 | - if ( ratings.length ) { | |
| 48 | - let scriptContents = ''; | |
| 50 | + ratings.forEach( ratingEl => { | |
| 51 | + if ( ratingEl.hasAttribute( 'data-pd-init-done' ) ) { | |
| 52 | + return; | |
| 53 | + } | |
| 49 | 54 | |
| 50 | - ratings.forEach( ratingEl => { | |
| 51 | - if ( ratingEl.hasAttribute( 'data-pd-init-done' ) ) { | |
| 52 | - return; | |
| 53 | - } | |
| 55 | + ratingEl.setAttribute( 'data-pd-init-done', '1' ); | |
| 54 | 56 | |
| 55 | - ratingEl.setAttribute( 'data-pd-init-done', '1' ); | |
| 57 | + const rating = parseJSON( ratingEl.getAttribute( 'data-settings' ) ); | |
| 56 | 58 | |
| 57 | - const rating = parseJSON( ratingEl.getAttribute( 'data-settings' ) ); | |
| 59 | + if ( ! rating ) { | |
| 60 | + return; | |
| 61 | + } | |
| 58 | 62 | |
| 59 | - if ( rating ) { | |
| 60 | - scriptContents += ` | |
| 61 | - PDRTJS_settings_${ rating.id }${ rating.item_id } = ${ rating.settings }; | |
| 62 | - if ( typeof PDRTJS_RATING !== 'undefined' ) { | |
| 63 | - if ( typeof PDRTJS_${ rating.id }${ rating.item_id } === 'undefined' ) { | |
| 64 | - PDRTJS_${ rating.id }${ rating.item_id } = | |
| 65 | - new PDRTJS_RATING( PDRTJS_settings_${ rating.id }${ rating.item_id } ); | |
| 66 | - } | |
| 67 | - } | |
| 68 | - `; | |
| 69 | - } | |
| 70 | - } ); | |
| 63 | + const settings = parseJSON( rating.settings ); | |
| 71 | 64 | |
| 72 | - if ( scriptContents ) { | |
| 73 | - const anchorEl = d.querySelector( '#polldaddyRatings' ); | |
| 74 | - if ( anchorEl ) { | |
| 75 | - const script = d.createElement( 'script' ); | |
| 76 | - script.id = 'polldaddyDynamicRatings'; | |
| 77 | - script.text = scriptContents; | |
| 65 | + if ( settings === null ) { | |
| 66 | + return; | |
| 67 | + } | |
| 78 | 68 | |
| 79 | - anchorEl.after( script ); | |
| 80 | - } | |
| 69 | + const key = `${ rating.id }${ rating.item_id }`; | |
| 70 | + w[ `PDRTJS_settings_${ key }` ] = settings; | |
| 71 | + | |
| 72 | + if ( | |
| 73 | + typeof w.PDRTJS_RATING !== 'undefined' && | |
| 74 | + typeof w[ `PDRTJS_${ key }` ] === 'undefined' | |
| 75 | + ) { | |
| 76 | + w[ `PDRTJS_${ key }` ] = new w.PDRTJS_RATING( w[ `PDRTJS_settings_${ key }` ] ); | |
| 81 | 77 | } |
| 82 | - } | |
| 78 | + } ); | |
| 83 | 79 | }, |
| 84 | 80 | }; |
| 85 | 81 | |
| 86 | 82 | d.body.addEventListener( 'is.post-load', () => w.polldaddyshortcode.render() ); |