PageRenderTime 36ms CodeModel.GetById 2ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/share/spice/forvo/spice.js

https://github.com/tarnfeld/zeroclickinfo-spice
JavaScript | 124 lines | 93 code | 31 blank | 0 comment | 19 complexity | 6eab9c11b3934c8ab6ce564fc2aaaeae MD5 | raw file
  1function ddg_spice_forvo(data) {
  2
  3    if (data.attributes.total != "0") {
  4
  5   	nrj("/forvo/jquery.min.js", true);
  6        nrj("/forvo/mediaelement-and-player.min.js", true);
  7        nrj("/forvo/init.js", true);
  8        
  9
 10        var words = data.items;
 11
 12        var wrap = d.createElement("div");
 13        
 14        out = '';
 15               
 16        for (var i = 0; i < 3 && i < words.length; i++){
 17            var word = words[i];
 18          
 19            var container = d.createElement("div");
 20            YAHOO.util.Dom.addClass(container, 'forvo_container');
 21
 22            var audio = d.createElement("audio");
 23            YAHOO.util.Dom.setAttribute(audio, 'preload', "none");
 24
 25            var mp3_source = d.createElement("source");
 26            mp3_source.src = word.standard_pronunciation.pathmp3 + ".mp3";
 27            mp3_source.type = "audio/mp3";
 28            
 29            var ogg_source = d.createElement("source");
 30            ogg_source.src = word.standard_pronunciation.pathogg + ".ogg";
 31            ogg_source.type = "audio/ogg";
 32
 33            audio.appendChild(mp3_source);
 34            audio.appendChild(ogg_source);
 35            
 36            var details_span = d.createElement("span");
 37            details_span.innerHTML = word.original;
 38            
 39            var details = d.createElement("div");
 40            YAHOO.util.Dom.addClass(details, 'forvo_track_details');
 41            details.appendChild(details_span);
 42            
 43            container.appendChild(audio);
 44            container.appendChild(details);
 45
 46            wrap.appendChild(container);
 47        }
 48
 49        out += wrap.innerHTML;
 50	
 51	var query = DDG.get_query();
 52	var replace = ["how to say", "how do you pronounce", "how do you say", "pronounciation of", "how to pronounce", "pronunciation", "pronounce", "forvo"];
 53
 54	for (i in replace){
 55		var phrase = replace[i];
 56		if (query.indexOf(phrase) != -1) {
 57			query = query.replace(phrase, '');
 58		}
 59	}
 60
 61	query = query.trim();
 62
 63	items = new Array();
 64        items[0] = new Array();
 65        items[0]['a'] = out;
 66        items[0]['h'] = 'Pronunciations by Forvo';
 67        items[0]['s'] = 'Forvo';
 68        items[0]['u'] = 'http://www.forvo.com/search/' + encodeURIComponent(query);
 69        items[0]['f'] = 1;
 70        items[0]['force_big_header'] = 1;
 71        nra(items, 1, 1);
 72
 73    } else {
 74    	var query = '';
 75    	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"];
 76    	var query_array = DDG.get_query().split(" ");
 77    	
 78    	for (var i=0; i < query_array.length; i++){
 79    		if (replace_words.indexOf(query_array[i]) === -1) {
 80    			if (query === ''){
 81    				query += query_array[i]
 82    			} else {
 83    				query += "  " + query_array[i];
 84    			}
 85    		}
 86    	}
 87    	
 88    	nrj("http://api.duckduckgo.com/?q=spell%20" + encodeURIComponent(query) + "&format=json&pretty=1&callback=handle_ddg_api");
 89    }
 90}
 91
 92function handle_ddg_api(data){
 93	if (data.AnswerType === "spell" && data.Answer && data.Answer.indexOf("right") === -1 && data.Answer.indexOf("No suggestions") === -1){
 94
 95		var array = data.Answer.replace(/,/g, "").split("</i>");
 96		var suggestions = array[1].split(" ");
 97
 98		var snippet = d.createElement("div");
 99		snippet.innerHTML = "Sorry, there were no pronunciations for your search. Did you mean to search for one of these?<br>";
100		var span = d.createElement("span");
101
102		for (var i=0; i < suggestions.length; i++){
103			var anchor = d.createElement("a");
104			anchor.innerHTML = suggestions[i];
105			anchor.href += "/?q=pronounce%20" + suggestions[i];
106			
107			span.appendChild(anchor);
108			if (i+1 != suggestions.length) {
109				span.innerHTML += ", ";
110			}
111		}
112
113		snippet.appendChild(span);
114
115		items = new Array();
116        items[0] = new Array();
117        items[0]['a'] = snippet.innerHTML;
118        items[0]['h'] = 'Forvo Disambiguations';
119        items[0]['s'] = 'Forvo';
120        items[0]['u'] = 'http://www.forvo.com/search/' + suggestions[0];
121        items[0]['force_big_header'] = 1;
122        nra(items, 0, 1);
123	}
124}