amarillo of kerria

ライティング練習。ブラウザがChromeなら画面を右クリックからも翻訳できるよ。

英語学習のためにアドオンを自作

Previously, I made an add-on that changes the default highlight color - like this:




This was very useful to me ... for the default color is not so much gentle to my eyes.


But I've been in need of another add-on, an add-on that can highlight a sentence of text one by one with an easy operation. Why?


I am still not a fluent reader of English. So in order to concentrate on one sentence at one time, I very often highlight the sentence as I read it. With one sentence finished, then I highlight the next one ... But this is a bit awkward and it takes a second or seconds every time I do it. So I needed a tool to facilitate doing such a way of reading. It's why I today tried to make such an add-on.


This add-on is used like this:


1. Double-click a part of text with Control Key pressed and you can highlight the rest of the sentence including that part of the text. (In this example, I double-clicked the word "Vicipaedia.")



2. Then press Control Key and you can alternate the highlight part to the next sentence.



3. If you press the Control Key with Shift Key pressed, the highligted text is extended up to the next sentence.



4. Click any place on the screen, the highlight is undone.



Note that I am a real amateur of Javascript ... So the code is very likely to be unseemly. But it seems to more or less work as far as I've used it on Google Chrome for a bit.


Other English learners can utilise this add-on ... So I put here the code as follows. (But being such an amateur, I myself can't really assess how safe or dangerous it is to use this code. Therefore if one needs an add-on with a similar function to this, it would be FAR safer to look somewhere else.)


{
   "name": "sentence-by-sentence highlighter",
   "version": "1.0.0",
   "description": "Double-click a part of text with Control Key pressed and you can highlight the rest of the sentence including that part of the text. Then press Control Key and you can alternate the highlight part to the next sentence. If you press the Control Key with Shift Key pressed, the highligted text is extended up to the next sentence.",
   "manifest_version": 2,
   "content_scripts": [
      {
         "matches": [ "http://*/*", "https://*/*" ],
         "js": [ "sentenceBySentenceHighlighter.js" ]
      }
   ]
}


var selectedText;
var isHighlighted = false;
 
document.addEventListener("dblclick", highlightSentence);
 
document.addEventListener("keydown", replaceSentence);
 
document.addEventListener("click", cancelHighlight);
 
function highlightSentence(e) {
   if (e.ctrlKey) {
      selectedText = window.getSelection();
      selectedText.modify("extend", "forward", "sentence");
      isHighlighted = true;
   }
}
 
function replaceSentence(e) {
   if (e.ctrlKey && isHighlighted) {
      selectedText = window.getSelection();
      if (!e.shiftKey) {
         selectedText.collapseToEnd();
      }
      selectedText.modify("extend", "forward", "sentence");
   }
}
 
function cancelHighlight() {
   if (isHighlighted) {
      isHighlighted = false;
   }
}


Copy and paste this code onto Notepad, then save these as a json file (manifest.json) and a Javascript file (sentenceBySentenceHighlighter.js) respectively, then put these two files together into a folder of any name. By doing this, you'll have prepared the folder to install onto Chrome as an add-on. The installation can be done via the "load unpackaged extensions" button shown in the picture below.



Of course, this is an amateur work, so I can't have any responsibility if you have any troubles by using this add-on. And it can't be guaranteed whether this can work on other settings or conditions.