Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Features added: 1. Search box will be pre-populated with the selected…

… text 2. Refactored the js code into app.js
  • Loading branch information...
commit b9d3c06837cab8e7cf1a890fde3195ae1ed73b62 1 parent 4d86310
@varunkumar varunkumar authored
Showing with 80 additions and 87 deletions.
  1. +60 −0 app.js
  2. +1 −1  manifest.json
  3. +13 −86 popup.html
  4. +6 −0 selection.js
View
60 app.js
@@ -0,0 +1,60 @@
+// Populate the search box with the text selected from the page
+chrome.tabs.getSelected(null, function(tab) {
+ chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function (response) {
+ $("#t1").val(response.data);
+ });
+});
+
+$("#t1").keypress(function(event) {
+ if ( event.which == 13 ) {
+ $("#search_btn").click();
+ }
+});
+
+$("#t1").focus();
+
+function random_color() {
+
+ var style = 'background: ';
+
+ var r, g, b;
+
+ r = Math.round(Math.random() * 0xFF);
+ g = Math.round(Math.random() * 0xFF);
+ b = Math.round(Math.random() * 0xFF);
+
+ style += 'rgba(' + r + ',' + g + ',' + b + ',1);';
+
+ /* The formula for calculating luminance is taken from
+ * http://www.paciellogroup.com/resources/contrast-analyser.html
+ *
+ * If there are better methods to change, please let me know.
+ */
+ var luminance = (r * 299 + g * 587 + b * 114 ) / 1000;
+
+ if (luminance < 125) {
+ style += 'color: #FFFFFF';
+ } else {
+ style += 'color: #000000';
+ }
+
+ return style;
+}
+
+function search(that) {
+
+ var token = new String (t1.value);
+
+ chrome.tabs.executeScript(null,
+ {code:"$(document.body).highlight('"+token+"','"+random_color()+"')"});
+
+ window.close();
+}
+
+
+function hl_clear(that) {
+ chrome.tabs.executeScript(null,
+ {code:"$(document.body).removeHighlight()"});
+
+ window.close();
+}
View
2  manifest.json
@@ -8,7 +8,7 @@
},
"content_scripts": [ {
"matches": [ "<all_urls>" ],
- "js": [ "jquery.js", "jquery.highlight-3.js" ]
+ "js": [ "jquery.js", "jquery.highlight-3.js", "selection.js" ]
} ],
"permissions": [
"tabs", "http://*/*", "https://*/*"
View
99 popup.html
@@ -1,93 +1,20 @@
-<script src="jquery.js"></script>
-<script src="jquery.highlight-3.js"></script>
-
-<script>
- function random_color() {
-
- var colo = 'background: #';
-
- var r, g, b;
-
- r = Math.round(Math.random() * 0xFF);
- g = Math.round(Math.random() * 0xFF);
- b = Math.round(Math.random() * 0xFF);
-
- var num;
-
- num = r.toString(16);
- if (num.length < 2) {
- colo += '0'; }
- colo += num;
-
- num = g.toString(16);
- if (num.length < 2) {
- colo += '0'; }
- colo += num;
-
- num = b.toString(16);
- if (num.length < 2) {
- colo += '0'; }
- colo += num;
-
- /* The formula for calculating luminance is taken from
- * http://www.paciellogroup.com/resources/contrast-analyser.html
- *
- * If there are better methods to change, please let me know.
- */
- var luminance = (r * 299 + g * 587 + b * 114 ) / 1000;
-
- if (luminance < 125) {
- colo += '; color: #FFFFFF';
- } else {
- colo += '; color: #000000';
- }
-
- return colo;
- }
-
- function search(that) {
-
- var token = new String (t1.value);
-
- chrome.tabs.executeScript(null,
- {code:"$(document.body).highlight('"+token+"','"+random_color()+"')"});
-
- window.close();
- }
-
-
- function hl_clear(that) {
-
- chrome.tabs.executeScript(null,
- {code:"$(document.body).removeHighlight()"});
-
- window.close();
- }
-
-</script>
-
<style>
-body {
- overflow: hidden;
- margin: 5px;
- padding: 0px;
- background: black;
- color: white;
- width: 300px;
- font-family: 'Droid Sans', arial, sans-serif;
-}
+ body {
+ overflow: hidden;
+ margin: 5px;
+ padding: 0px;
+ background: black;
+ color: white;
+ width: 300px;
+ font-family: 'Droid Sans', arial, sans-serif;
+ }
</style>
Please enter the word to highlight :</br></br>
<input type="text" id="t1"/></br></br>
<button onclick="search(this)" id="search_btn">Search</button>
-<script>
-$("#t1").keypress(function(event) {
- if ( event.which == 13 ) {
- $("#search_btn").click();
- }
-});
-</script>
-
<button onclick="hl_clear(this)" id ="clear_btn">Clear all highlights</button>
-<script>t1.focus()</script>
+
+<script src="jquery.js"></script>
+<script src="jquery.highlight-3.js"></script>
+<script src="app.js"></script>
View
6 selection.js
@@ -0,0 +1,6 @@
+chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
+ if (request.method == "getSelection")
+ sendResponse({data: window.getSelection().toString()});
+ else
+ sendResponse({}); // snub them.
+});
Please sign in to comment.
Something went wrong with that request. Please try again.