PageRenderTime 1ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/share/spice/forvo/spice.js

https://github.com/aembleton/zeroclickinfo-spice
JavaScript | 123 lines | 93 code | 30 blank | 0 comment | 19 complexity | 9ed6795ffee8f4bae7219a7c9718a732 MD5 | raw file
  1nrj("/forvo/jquery.min.js", true);
  2nrj("/forvo/mediaelement-and-player.min.js", true);
  3nrj("/forvo/init.js", true);
  4
  5function ddg_spice_forvo(data) {
  6
  7    if (data.attributes.total != "0") {
  8
  9        var words = data.items;
 10
 11        var wrap = d.createElement("div");
 12        
 13        out = '';
 14               
 15        for (var i = 0; i < 3 && i < words.length; i++){
 16            var word = words[i];
 17          
 18            var container = d.createElement("div");
 19            YAHOO.util.Dom.addClass(container, 'forvo_container');
 20
 21            var audio = d.createElement("audio");
 22            YAHOO.util.Dom.setAttribute(audio, 'preload', "none");
 23
 24            var mp3_source = d.createElement("source");
 25            mp3_source.src = word.standard_pronunciation.pathmp3 + ".mp3";
 26            mp3_source.type = "audio/mp3";
 27            
 28            var ogg_source = d.createElement("source");
 29            ogg_source.src = word.standard_pronunciation.pathogg + ".ogg";
 30            ogg_source.type = "audio/ogg";
 31
 32            audio.appendChild(mp3_source);
 33            audio.appendChild(ogg_source);
 34            
 35            var details_span = d.createElement("span");
 36            details_span.innerHTML = word.original;
 37            
 38            var details = d.createElement("div");
 39            YAHOO.util.Dom.addClass(details, 'forvo_track_details');
 40            details.appendChild(details_span);
 41            
 42            container.appendChild(audio);
 43            container.appendChild(details);
 44
 45            wrap.appendChild(container);
 46        }
 47
 48        out += wrap.innerHTML;
 49	
 50	var query = DDG.get_query();
 51	var replace = ["how to say", "how do you pronounce", "how do you say", "pronounciation of", "how to pronounce", "pronunciation", "pronounce", "forvo"];
 52
 53	for (i in replace){
 54		var phrase = replace[i];
 55		if (query.indexOf(phrase) != -1) {
 56			query = query.replace(phrase, '');
 57		}
 58	}
 59
 60	query = query.trim();
 61
 62	items = new Array();
 63        items[0] = new Array();
 64        items[0]['a'] = out;
 65        items[0]['h'] = 'Pronunciations by Forvo';
 66        items[0]['s'] = 'Forvo';
 67        items[0]['u'] = 'http://www.forvo.com/search/' + encodeURIComponent(query);
 68        items[0]['force_no_fold'] = 1;
 69        items[0]['force_big_header'] = 1;
 70        nra(items, 1, 1);
 71
 72    } else {
 73    	var query = '';
 74    	var replace_words = ["forvo", "pronounce", "in","how", "to", "abkhazian", "adygean", "afar", "afrikaans", "akan", "albanian", "amharic", "ancient greek", "arabic", "aragonese", "arb&#235;resh", "armenian", "assamese", "assyrian neo-aramaic", "asturian", "avaric", "aymara", "azerbaijani", "balochi", "bambara", "bardi", "bashkir", "basque", "bavarian", "belarusian", "bengali", "bihari", "bislama", "bosnian", "botlikh", "breton", "bulgarian", "buriat", "burmese", "burushaski", "cantonese", "cape verdean creole", "catalan", "cebuano", "central bikolano", "chamorro", "chechen", "cherokee", "chichewa", "chuvash", "cornish", "corsican", "cree", "creek", "crimean tatar", "croatian", "czech", "dagbani", "danish", "dari", "divehi", "dutch", "dzongkha", "emilian", "english", "erzya", "esperanto", "estonian", "eton", "ewe", "ewondo", "faroese", "fijian", "finnish", "flemish", "franco-proven&#231;al", "french", "friulan", "fulah", "galician", "gan chinese", "georgian", "german", "gilaki", "greek", "guarani", "gujarati", "haitian", "hakka", "hassaniyya", "hausa", "hawaiian", "hebrew", "herero", "hiligaynon", "hindi", "hiri motu", "hmong", "hungarian", "icelandic", "igbo", "iloko", "indonesian", "ingush", "interlingua", "inuktitut", "inupiaq", "irish", "italian", "iwaidja", "jamaican patois", "japanese", "javanese", "jin chinese", "kabardian", "kabyle", "kalaallisut", "kalmyk", "kannada", "kanuri", "karakalpak", "kashmiri", "kazakh", "khasi", "khmer", "kikuyu", "kimbundu", "kinyarwanda", "kirundi", "klingon", "komi", "kongo", "konkani", "korean", "kotava", "krio", "kuanyama", "kurdish", "kutchi", "kyrgyz", "lakota", "lao", "latin", "latvian", "lezgian", "ligurian", "limburgish", "lingala", "lithuanian", "lombard", "low german", "lozi", "luba-katanga", "luganda", "luo", "lushootseed", "luxembourgish", "macedonian", "mainfr&#228;nkisch", "malagasy", "malay", "malayalam", "maltese", "mandarin chinese", "manx", "m&#257;ori", "mapudungun", "marathi", "marshallese", "masbate&#241;o", "mauritian creole", "mazandarani", "mbe", "meitei", "micmac", "min dong", "min nan", "minangkabau", "mohawk", "moksha", "mongolian", "nahuatl", "nauru", "navajo", "naxi", "ndonga", "neapolitan", "nepal bhasa", "nepali", "nogai", "north ndebele", "northern sami", "norwegian bokm&#229;l", "norwegian nynorsk", "nuosu", "n&#448;uu", "occitan", "ojibwa", "okinawan", "oriya", "oromo", "osage", "ossetian", "ottoman turkish", "palauan", "palenquero", "pangasinan", "panjabi", "papiamento", "pashto", "pennsylvania dutch", "persian", "picard", "piedmontese", "pitjantjatjara", "polish", "portuguese", "pulaar", "quechua", "quiatoni zapotec", "rapa nui", "romagnol", "romani", "romanian", "romansh", "rukiga", "russian", "rusyn", "samoan", "sango", "sanskrit", "sardinian", "scots", "scottish gaelic", "serbian", "serer", "shona", "shoshoni", "sicilian", "silesian", "sindhi", "sinhalese", "slovak", "slovenian", "somali", "sotho", "south ndebele", "spanish", "sranan tongo", "sundanese", "swabian german", "swahili", "swati", "swedish", "swiss german", "sylheti", "tagalog", "tahitian", "tajik", "tamil", "tatar", "telugu", "tetum", "thai", "tibetan", "tigrinya", "tok pisin", "toki pona", "tondano", "tonga", "tsonga", "tswana", "tuareg", "tundra nenets", "turkish", "turkmen", "tuscarora", "tuvan", "twi", "udmurt", "uighur", "ukrainian", "upper sorbian", "urdu", "uzbek", "valencian", "venda", "venetian", "vietnamese", "v&#245;ro", "walloon", "welsh", "western frisian", "wolof", "wu chinese", "xhosa", "xiang chinese", "yakut", "yiddish", "yoruba", "yucatec maya", "yupik", "zazaki", "zhuang", "zulu"];
 75    	var query_array = DDG.get_query().split(" ");
 76    	
 77    	for (var i=0; i < query_array.length; i++){
 78    		if (replace_words.indexOf(query_array[i]) === -1) {
 79    			if (query === ''){
 80    				query += query_array[i]
 81    			} else {
 82    				query += "  " + query_array[i];
 83    			}
 84    		}
 85    	}
 86    	
 87    	nrj("http://api.duckduckgo.com/?q=spell%20" + encodeURIComponent(query) + "&format=json&pretty=1&callback=handle_ddg_api");
 88    }
 89}
 90
 91function handle_ddg_api(data){
 92	if (data.AnswerType === "spell" && data.Answer && data.Answer.indexOf("right") === -1 && data.Answer.indexOf("No suggestions") === -1){
 93
 94		var array = data.Answer.replace(/,/g, "").split("</i>");
 95		var suggestions = array[1].split(" ");
 96
 97		var snippet = d.createElement("div");
 98		snippet.innerHTML = "Sorry, there were no pronunciations for your search. Did you mean to search for one of these?<br>";
 99		var span = d.createElement("span");
100
101		for (var i=0; i < suggestions.length; i++){
102			var anchor = d.createElement("a");
103			anchor.innerHTML = suggestions[i];
104			anchor.href += "/?q=pronounce%20" + suggestions[i];
105			
106			span.appendChild(anchor);
107			if (i+1 != suggestions.length) {
108				span.innerHTML += ", ";
109			}
110		}
111
112		snippet.appendChild(span);
113
114		items = new Array();
115        items[0] = new Array();
116        items[0]['a'] = snippet.innerHTML;
117        items[0]['h'] = 'Forvo Disambiguations';
118        items[0]['s'] = 'Forvo';
119        items[0]['u'] = 'http://www.forvo.com/search/' + suggestions[0];
120        items[0]['force_big_header'] = 1;
121        nra(items, 0, 1);
122	}
123}