There are no APIs to access Chrome spellcheck suggestions, and there are no events that would fire when words were saddened. However, events can be realized.
I have no idea what your use case for this function is, but I put together a demo using the montanaflynn spellchecker API in MashApe . The demo scans the text area, and when the user pauses when entering text, he sends the text through the API being checked. The API returns a JSON containing the source string, the proposed corrected string, and an object containing the corrected words and the suggested replacements.
Offers are displayed below the text box. When sentences freeze, a hazy word is highlighted. When a button is pressed, a typo is replaced by a sentence.
I also added a shuffle function that scrambles the words in the string before sending it to add a privacy layer to the use of the API (it also uses SSL). Neither the API nor Chrome use contextual suggestions, so shuffling does not change the results.
Here's a link to CodePen: http://codepen.io/aecend/pen/rOebQq
And here is the code:
CSS
<style> * { font-family: sans-serif; } textarea { margin-bottom: 10px; width: 500px; height: 300px; padding: 10px; } .words { width: 500px; } .word { display: inline-block; padding: 2px 5px 1px 5px; border-radius: 2px; background: #00B1E6; color: white; margin: 2px; cursor: pointer; } </style>
HTML
<textarea id="text" placeholder="Type something here..."></textarea> <div id="words"></div>
Javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script> ;(function(){ "use strict"; var words = document.getElementById("words"), input = document.getElementById("text"), timeout, xhr; input.addEventListener("keyup", function(e){ if (timeout) clearTimeout(timeout); if (!this.value.trim()) words.innerHTML = ''; timeout = setTimeout(function() { var test_phrase = shuffle_words( input.value ); spell_check(test_phrase); timeout = null; }, 500); }); function shuffle_words(inp) { inp = inp.replace(/\s+/g, ' '); var arr = inp.split(" "), n = arr.length; while (n > 0) { var i = Math.floor(Math.random() * n--), t = arr[n]; arr[n] = arr[i]; arr[i] = t; } return arr.join(' '); } function spell_check(text){ if (xhr) xhr.abort(); xhr = $.ajax({ url: 'https://montanaflynn-spellcheck.p.mashape.com/check/', headers: { 'X-Mashape-Key': 'U3ogA8RAAMmshGOJkNxkTBbuYYRTp1gMAuGjsniThZuaoKIyaj', 'Accept': 'application/json' }, data: { 'text': text }, cache: false, success: function(result){ xhr = null; suggest_words(result); } }); } function suggest_words(obj){ if (!obj.corrections) return; words.innerHTML = ''; for (var key in obj.corrections) { if (obj.corrections.hasOwnProperty(key)) { var div = document.createElement("div"); div.className = "word"; div.innerHTML = obj.corrections[key][0]; div.orig = key; div.onmouseover = function() { var start = input.value.indexOf(this.orig); input.selectionStart = start; input.selectionEnd = start + this.orig.length; }; div.onmouseout = function() { var len = input.value.length; input.selectionStart = len; input.selectionEnd = len; } div.onclick = function() { input.value = input.value.replace(this.orig, this.innerHTML); this.parentNode.removeChild(this); } words.appendChild(div); } } } })(); </script>
I used jQuery to simplify the AJAX request for this demo. This can easily be done in JS Vanilla.