| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Processes select tag that contains list of available terms for different | 6 * Processes select tag that contains list of available terms for different |
| 7 * languages and zones. In case of initial load, tries to find terms that match | 7 * languages and zones. In case of initial load, tries to find terms that match |
| 8 * exactly current language and country code and automatically redirects the | 8 * exactly current language and country code and automatically redirects the |
| 9 * view in case such terms are found. Leaves terms in select tag that only match | 9 * view in case such terms are found. Leaves terms in select tag that only match |
| 10 * current language or country code or default English variant or currently | 10 * current language or country code or default English variant or currently |
| 11 * selected. Note that document.countryCode must be set before calling this | 11 * selected. Note that document.countryCode must be set before calling this |
| 12 * function. | 12 * function. |
| 13 */ | 13 */ |
| 14 function processLangZoneTerms() { | 14 function processLangZoneTerms() { |
| 15 var doc = document; | 15 var doc = document; |
| 16 var selectLangZoneTerms = doc.getElementById('play-footer'). | 16 var selectLangZoneTerms = doc.getElementById('play-footer'). |
| 17 getElementsByTagName('select')[0]; | 17 getElementsByTagName('select')[0]; |
| 18 | 18 |
| 19 if (window.location.href == 'https://play.google.com/about/play-terms.html') { | 19 var initialLoad = window.location.href == |
| 20 var matchByLangZone = '/intl/' + navigator.language + '_' + | 20 'https://play.google.com/about/play-terms.html'; |
| 21 document.countryCode + '/about/play-terms.html'; | 21 var langSegments = navigator.language.split('-'); |
| 22 for (var i = selectLangZoneTerms.options.length - 1; i >= 0; --i) { | 22 if (initialLoad) { |
| 23 var option = selectLangZoneTerms.options[i]; | 23 var applyTermsForLangAndZone = function(termsLang) { |
| 24 if (option.value == matchByLangZone) { | 24 var matchByLangZone = '/intl/' + termsLang + '_' + |
| 25 window.location.href = option.value; | 25 document.countryCode + '/about/play-terms.html'; |
| 26 return; | 26 for (var i = selectLangZoneTerms.options.length - 1; i >= 0; --i) { |
| 27 var option = selectLangZoneTerms.options[i]; |
| 28 if (option.value == matchByLangZone) { |
| 29 window.location.href = option.value; |
| 30 return true; |
| 31 } |
| 27 } | 32 } |
| 33 return false; |
| 34 }; |
| 35 |
| 36 // Try two versions of the language, full and short (if it exists, for |
| 37 // example en-GB -> en). Note, terms may contain entries for both types, for |
| 38 // example: en_ie, es-419_ar, es_as, pt-PT_pt. |
| 39 if (applyTermsForLangAndZone(navigator.language)) { |
| 40 return; |
| 41 } |
| 42 if (langSegments.length == 2 && |
| 43 applyTermsForLangAndZone(langSegments[0])) { |
| 44 return; |
| 28 } | 45 } |
| 29 } | 46 } |
| 30 | 47 |
| 31 var matchByLang = '/intl/' + navigator.language + '_'; | 48 var matchByLang = '/intl/' + navigator.language + '_'; |
| 49 var matchByLangShort = null; |
| 50 if (langSegments.length == 2) { |
| 51 matchByLangShort = '/intl/' + langSegments[0] + '_'; |
| 52 } |
| 53 |
| 32 var matchByZone = '_' + document.countryCode + '/about/play-terms.html'; | 54 var matchByZone = '_' + document.countryCode + '/about/play-terms.html'; |
| 33 var matchByDefault = '/intl/en/about/play-terms.html'; | 55 var matchByDefault = '/intl/en/about/play-terms.html'; |
| 34 | 56 |
| 57 // We are allowed to display terms by default only in language that matches |
| 58 // current UI language. In other cases we have to switch to default version. |
| 59 var langMatch = false; |
| 60 var defaultExist = false; |
| 61 |
| 35 for (var i = selectLangZoneTerms.options.length - 1; i >= 0; --i) { | 62 for (var i = selectLangZoneTerms.options.length - 1; i >= 0; --i) { |
| 36 var option = selectLangZoneTerms.options[i]; | 63 var option = selectLangZoneTerms.options[i]; |
| 37 if (selectLangZoneTerms.selectedIndex != i && | 64 if (selectLangZoneTerms.selectedIndex == i) { |
| 38 !option.value.startsWith(matchByLang) && | 65 langMatch = option.value.startsWith(matchByLang) || |
| 66 (matchByLangShort && option.value.startsWith(matchByLangShort)); |
| 67 continue; |
| 68 } |
| 69 if (option.value == matchByDefault) { |
| 70 defaultExist = true; |
| 71 continue; |
| 72 } |
| 73 if (!option.value.startsWith(matchByLang) && |
| 39 !option.value.endsWith(matchByZone) && | 74 !option.value.endsWith(matchByZone) && |
| 40 option.value != matchByDefault && option.text != 'English') { | 75 !(matchByLangShort && option.value.startsWith(matchByLangShort)) && |
| 76 option.text != 'English') { |
| 41 selectLangZoneTerms.removeChild(option); | 77 selectLangZoneTerms.removeChild(option); |
| 42 } | 78 } |
| 43 } | 79 } |
| 44 // Show content once we reached target url. | 80 if (initialLoad && !langMatch && defaultExist) { |
| 45 document.body.hidden = false; | 81 window.location.href = matchByDefault; |
| 82 } else { |
| 83 // Show content once we reached target url. |
| 84 document.body.hidden = false; |
| 85 } |
| 46 } | 86 } |
| 47 | 87 |
| 48 /** | 88 /** |
| 49 * Formats current document in order to display it correctly. | 89 * Formats current document in order to display it correctly. |
| 50 */ | 90 */ |
| 51 function formatDocument() { | 91 function formatDocument() { |
| 52 // playstore.css is injected into the document and it is applied first. | 92 // playstore.css is injected into the document and it is applied first. |
| 53 // Need to remove existing links that contain references to external | 93 // Need to remove existing links that contain references to external |
| 54 // stylesheets which override playstore.css. | 94 // stylesheets which override playstore.css. |
| 55 var links = document.head.getElementsByTagName('link'); | 95 var links = document.head.getElementsByTagName('link'); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 var doc = document; | 107 var doc = document; |
| 68 document.body.removeChild(doc.getElementById('play-header')); | 108 document.body.removeChild(doc.getElementById('play-header')); |
| 69 | 109 |
| 70 // Hide content at this point. We might want to redirect our view to terms | 110 // Hide content at this point. We might want to redirect our view to terms |
| 71 // that exactly match current language and country code. | 111 // that exactly match current language and country code. |
| 72 document.body.hidden = true; | 112 document.body.hidden = true; |
| 73 } | 113 } |
| 74 | 114 |
| 75 formatDocument(); | 115 formatDocument(); |
| 76 processLangZoneTerms(); | 116 processLangZoneTerms(); |
| OLD | NEW |