建立一個簡單的 Chrome Extension ,按右鍵會撥放選取範圍的文字
manifest.json
{ "manifest_version" :2, "name" : "AudioPlayer", "version" : "1.0", "description" : "播放英文", "icons" :{ "128" : "icon128.png", "48" : "icon48.png", "16" : "icon16.png" }, "background" :{ "scripts" :["eventPage.js"], "persistent" : false }, "permissions" : [ "contextMenus" ] }
eventPage.js
var contextMenuItem = { "id" : "AudioPlayer", "title" : "Play : %s", "contexts" : ["selection"] }; chrome.contextMenus.create(contextMenuItem); chrome.contextMenus.onClicked.addListener(function(clickData){ if(clickData.menuItemId=="AudioPlayer"){ var selection = clickData.selectionText; var myAudio = new Audio(); myAudio.src = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q="+selection+"&tl=en&client=tw-ob"; myAudio.play(); } });
很簡單的創建 audio 並使用 google translate 播放文字