Skip to content

Commit

Permalink
Fix compatibility with newer extention API version (for Firefox)
Browse files Browse the repository at this point in the history
- Move all functions to content script.
- Some design changes.
  • Loading branch information
bitim committed Sep 30, 2016
1 parent 79bbaa2 commit 07e9ddb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 70 deletions.
38 changes: 0 additions & 38 deletions HTT-background.js

This file was deleted.

52 changes: 47 additions & 5 deletions HTT-contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@
var HTTdefinitions = ""; //translation output
var HTToptions;


function xhr(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = xhr.responseText;

callback(data);
} else {
callback(null);
}
}
}
// Note that any URL fetched here must be matched by a permission in
// the manifest.json file!
xhr.open('GET', url, true);
xhr.send();
};

/**
* Handles data sent via chrome.extension.sendRequest().
* @param request Object Data sent in the request.
* @param sender Object Origin of the request.
* @param callback Function The method to call when the request completes.
*/
function onRequest(request, callback) {
if (request.action == 'xhr') {
xhr(request.url, callback);
} else if(request.action == 'localStorage_set') {
localStorage[request.attribute] = JSON.stringify(request.value || null);

callback();
} else if(request.action == 'localStorage_get') {
callback(JSON.parse(localStorage[request.attribute] || null) || null);
}
};


//helper functions
function appendChild(child,parent){return(parent.insertBefore(child,parent.lastChild.nextSibling));}

Expand Down Expand Up @@ -116,7 +155,7 @@
if(HTToptions['activity_indicator']) {
HTTtooltip.style.width = "auto";
HTTtooltip.style.height = "auto";
HTTtooltip.innerHTML = "<span class='HTT HTTActivityIndicator'>HTT...</span>"
HTTtooltip.innerHTML = "<div class='HTT HTTActivityIndicator'></div>"
ttX = HTTcurX;
ttY = HTTcurY;
if(HTToptions['align_left']) {
Expand Down Expand Up @@ -149,15 +188,15 @@
HTTtooltip.style.visibility="visible";
}
var HTTreq;

chrome.extension.sendRequest({'action' : 'xhr', 'url' : 'http://www.morfix.co.il/' + encodeURIComponent(input)}, HTTparseResponse);
onRequest({'action' : 'xhr', 'url' : 'http://www.morfix.co.il/' + encodeURIComponent(input)}, HTTparseResponse);
}
}

function getStringOffsetFromPoint(elem, x, y) {
try {
if(elem.nodeType == elem.TEXT_NODE) {
var range = elem.ownerDocument.createRange();

range.selectNodeContents(elem);
var str = range.toString();
var currentPos = 0;
Expand All @@ -166,6 +205,9 @@
while(currentPos < endPos) {
range.setStart(elem, currentPos);
range.setEnd(elem, currentPos+1);

var tmp = range.getBoundingClientRect();

if(range.getBoundingClientRect() &&
range.getBoundingClientRect().left <= x && range.getBoundingClientRect().right >= x &&
range.getBoundingClientRect().top <= y && range.getBoundingClientRect().bottom >= y) {
Expand Down Expand Up @@ -351,8 +393,8 @@
return;
}

function HTTinit () {
chrome.extension.sendRequest({'action' : 'localStorage_get', 'attribute' : 'options'}, HTToptions_callback);
function HTTinit() {
onRequest({'action' : 'localStorage_get', 'attribute' : 'options'}, HTToptions_callback);
//don't continue until the callback completes
}

Expand Down
Binary file added ajax-loader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions htt.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
.HTTWord { font-size:11pt; color:#000099; }
.HTTPartOfSpeech { font-size:7pt; color:black; font-family:Arial; }
.HTTDefinition { font-size:11pt; color:#000099; }
.HTTtooltip { border: 1px solid black; visibility: hidden; top: 0px; left: 0px; position: fixed; background-color: lightyellow; z-index: 10000; width: 0px; height: 0px; }
.HTTActivityIndicator { font-size: 7pt; direction:ltr; }
.HTTtooltip { border: 2px solid #e1ecff; visibility: hidden; top: 0px; left: 0px; position: fixed; background-color: #F7FAFF; z-index: 10000; width: 0px; height: 0px; padding: 4px; border-radius: 5px; max-width:400px}
.HTTActivityIndicator { background-image: url(ajax-loader.gif); height: 24px; width: 24px;}
52 changes: 27 additions & 25 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{
"name": "Hebrew Tooltip Translation for Google Chrome™",
"version": "2.6",
"manifest_version": 2,
"description": "Translate Hebrew to English and vice-versa via tooltip",
"permissions": [
"http://www.morfix.co.il/*"
],
"icons": {
"16": "HTT-16.png",
"48": "HTT-48.png",
"128": "HTT-128.png"
},
"background" : {
"scripts": ["HTT-background.js"],
"persistent": true
},
"options_page": "HTT-options.html",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"css": ["htt.css"],
"js" : ["HTT-contentscript.js"]
{
"update_url": "https://clients2.google.com/service/update2/crx",
"name": "Hebrew Tooltip Translation for Google Chrome™",
"version": "2.6",
"manifest_version": 2,
"description": "Translate Hebrew to English and vice-versa via tooltip",
"permissions": ["http://www.morfix.co.il/*"],
"icons": {
"16": "HTT-16.png",
"48": "HTT-48.png",
"128": "HTT-128.png"
},
"background": {
"scripts": ["HTT-background.js"],
"persistent": true
},
"options_page": "HTT-options.html",
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"css": ["htt.css"],
"js": ["HTT-contentscript.js"]
}],
"applications": {
"gecko": {
"id": "mhbjoppmeodjnkmbekpmfgicgiaecblm@chrome-store"
}
}
]
}
}

0 comments on commit 07e9ddb

Please sign in to comment.