Skip to content

Commit 1ade60b

Browse files
authored
Merge pull request #2 from csoni111/master
Added jQuery and dynamically positioning the overlay.
2 parents bee53e0 + bacec37 commit 1ade60b

File tree

8 files changed

+107
-73
lines changed

8 files changed

+107
-73
lines changed

content_script.css

Lines changed: 0 additions & 14 deletions
This file was deleted.

content_script.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

css/content_script.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.box_overlay {
2+
position: absolute;
3+
top: 0;
4+
left: 0;
5+
background-color: #ffff99;
6+
color: black;
7+
text-align: center;
8+
border-radius: 6px;
9+
font-size: 14px;
10+
max-width: 300px;
11+
z-index: 1000;
12+
opacity: 0;
13+
padding: 0;
14+
height: 0;
15+
width: 100%;
16+
transition: height 0.8s,opacity 0.5s,left 0.5s,top 0.5s;
17+
}
18+
.box_overlay.visible {
19+
opacity: 0.9;
20+
height: auto;
21+
padding: 10px;
22+
}
23+
.box_overlay.visible:hover {
24+
opacity: 1;
25+
}
26+
.box_overlay.visible::selection {
27+
background: #ffb7b7;
28+
}

images/icon48_disabled.png

2.42 KB
Loading

background.js renamed to js/background.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
var result = null;
2+
var active = true;
3+
4+
chrome.browserAction.onClicked.addListener(function(tab) {
5+
if(active) {
6+
chrome.browserAction.setTitle({title:"Click to Enable"});
7+
chrome.browserAction.setIcon({path:"images/icon48_disabled.png"});
8+
} else {
9+
chrome.browserAction.setTitle({title:"Click to Disable"});
10+
chrome.browserAction.setIcon({path:"images/icon48.png"});
11+
}
12+
active = !active;
13+
})
214

315
chrome.extension.onMessage.addListener(function(request, sender){
4-
getMeaning(request.message);
16+
if(active) {
17+
getMeaning(request.message);
18+
} else {
19+
result = {};
20+
result["active"] = active;
21+
sendResult(result);
22+
}
523
});
624

725
function getMeaning(word){
826
var url = "http://services.aonaware.com/DictService/DictService.asmx/Define?word="+String(word);
9-
var xmlHttp = new XMLHttpRequest();
10-
xmlHttp.onreadystatechange = function() {
11-
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
12-
parseXmlData(word,xmlHttp.responseXML);
13-
}
14-
}
15-
xmlHttp.open("GET", url, true);
16-
xmlHttp.send(null);
27+
var xmlHttp = new XMLHttpRequest();
28+
xmlHttp.onreadystatechange = function() {
29+
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
30+
parseXmlData(word,xmlHttp.responseXML);
31+
}
32+
}
33+
xmlHttp.open("GET", url, true);
34+
xmlHttp.send(null);
1735
}
1836

1937
function parseXmlData(word,xmlDoc){
@@ -51,12 +69,13 @@ function parseXmlData(word,xmlDoc){
5169
}
5270

5371
result["meaning"] = meaning;
72+
result["active"] = active;
5473
sendResult(result);
5574
}
5675

5776
function sendResult(result){
5877
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
59-
chrome.tabs.sendMessage(tabs[0].id, result);
78+
chrome.tabs.sendMessage(tabs[0].id, result);
6079
});
6180
}
6281

js/content_script.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var $x=0,$y=0;
2+
// Add bubble to the top of the page.
3+
var $box = $("<div>", {"class": "box_overlay"});
4+
$("body").append($box);
5+
$box = $(".box_overlay");
6+
7+
$("body").dblclick(function(e){
8+
var selectedText = window.getSelection().toString().trim();
9+
if (selectedText.length > 0) {
10+
$x = $(window).scrollLeft() + e.clientX;
11+
$y = $(window).scrollTop() + e.clientY;
12+
chrome.runtime.sendMessage({message: selectedText});
13+
}
14+
});
15+
16+
// Close the bubble when we click on the screen.
17+
$("body :not(.box_overlay)").on("click", function () {
18+
$box.removeClass("visible");
19+
$box.html("");
20+
});
21+
22+
chrome.runtime.onMessage.addListener(function(request, sender) {
23+
if(!sender.tab) {
24+
if(request.active) {
25+
$box.html(request.meaning);
26+
}
27+
}
28+
});
29+
30+
$box.bind("DOMSubtreeModified",function(){
31+
$box.addClass("visible");
32+
$x -= $(this).width()/2;
33+
$y -= $(this).height() + 50;
34+
if($x<0)
35+
$x=0;
36+
if($y<0)
37+
$y += $(this).height() + 60;
38+
$(this).offset({top:($y>0?$y:0),left:$x});
39+
});

js/jquery-1.12.4.min.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifest.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
"version": "1.1.0",
77

88
"background": {
9-
"scripts": ["background.js"],
9+
"scripts": ["js/background.js"],
1010
"persistent": false
1111
},
1212

1313
"content_scripts": [{
1414
"matches": ["<all_urls>"],
15-
"css": ["content_script.css"],
16-
"js": ["content_script.js"],
15+
"css": ["css/content_script.css"],
16+
"js": ["js/jquery-1.12.4.min.js","js/content_script.js"],
1717
"run_at": "document_end"
1818
}],
1919

20+
"browser_action": {
21+
"default_icon": "images/icon48.png",
22+
"default_title": "Click to Disable" },
2023

2124
"icons": {
2225
"16": "images/icon16.png",

0 commit comments

Comments
 (0)