Permalink
Browse files

Add change icon when clicked functionality

  • Loading branch information...
1 parent 7893e89 commit 62c9174cd1231e1d903ac7099870b89878004e15 @wilbertliu committed May 9, 2015
Showing with 50 additions and 2 deletions.
  1. +14 −0 background.js
  2. +17 −0 content.js
  3. +4 −0 jquery-2.1.4.min.js
  4. +15 −2 manifest.json
View
@@ -0,0 +1,14 @@
+chrome.browserAction.onClicked.addListener(function(tab) {
+ chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
+ var activeTab = tabs[0];
+ chrome.tabs.sendMessage(activeTab.id, { "message": "clicked_browser_action" });
+ });
+});
+
+chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+ // Returned from content script that should contain current_icon_path
+ if (request.message == "clicked_browser_action") {
+ // Truly change the extension's icon
+ chrome.browserAction.setIcon({ path: { "38": request.current_icon_path }, tabId: sender.tab.id });
+ }
+});
View
@@ -0,0 +1,17 @@
+var hidden_icon_name_html = "<input type='hidden' value='mata-inactive.png' id='mata-icon-name' />";
+
+$('body').append(hidden_icon_name_html);
+
+chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+ // Extension is clicked
+ if (request.message == "clicked_browser_action") {
+ // Change the extension's icon name on hidden field
+ if ($('#mata-icon-name').val() == "mata-inactive.png") {
+ $('#mata-icon-name').val("mata-active.png");
+ } else {
+ $('#mata-icon-name').val("mata-inactive.png");
+ }
+
+ chrome.runtime.sendMessage({ "message": "clicked_browser_action", "current_icon_path": $('#mata-icon-name').val() });
+ }
+});
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -6,6 +6,19 @@
"version": "0.0.1",
"browser_action": {
- "default_icon": "mata-inactive.png"
- }
+ "default_icon": { "38": "mata-inactive.png" }
+ },
+
+ "background": {
+ "scripts": ["background.js"]
+ },
+
+ "content_scripts": [
+ {
+ "matches": [
+ "<all_urls>"
+ ],
+ "js": ["jquery-2.1.4.min.js", "content.js"]
+ }
+ ]
}

0 comments on commit 62c9174

Please sign in to comment.