From 2f18d6d11c12f19a2a9e3d4a8eb19a2eee844cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pan=20Blbej=20Tu=C5=88=C3=A1k?= Date: Sun, 1 Mar 2015 12:04:36 +0100 Subject: [PATCH] ADD support for modx chrome extra --- src/background.js | 41 +++++++++++++++++++++++++++++++++++++---- src/content.js | 37 ++++++++++++++++++++++++++----------- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/background.js b/src/background.js index cf91308..d34df16 100644 --- a/src/background.js +++ b/src/background.js @@ -1,3 +1,4 @@ +// Actions onClicked chrome.browserAction.onClicked.addListener(function(tab) { var tab_url = tab.url; var tabIndex = tab.index; @@ -11,7 +12,7 @@ chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.sendMessage(tab.id, { - command: "getResource" + command: "getManager" }, function(docid) { @@ -23,12 +24,44 @@ chrome.browserAction.onClicked.addListener(function(tab) { chrome.browserAction.setTitle({title:"Preview MODX website"}); } else { newUrl = base; - } + } - //chrome.browserAction.setIcon ({path: 'images/icon128.png'}); chrome.tabs.create({ url: newUrl, index: tabIndex + 1 }); }); -}); \ No newline at end of file +}); + +// Change icon onMessage +var badge_color = [0, 0, 0, 255]; +var success_color = [95, 181, 77, 255]; +var error_color = [229, 62, 48, 255]; + + +chrome.runtime.onMessage.addListener( + function(request, sender, sendResponse) { + + if (request.published == '0') badge_color = error_color; + if (request.published == '1') badge_color = success_color; + + var tooltip = []; + if (request.docid) tooltip.push('docid: ' + request.docid); + if (request.published) tooltip.push('published: ' + request.published); + if (request.editedby) tooltip.push('editedby: ' + request.editedby); + if (request.editedon) tooltip.push('editedon: ' + request.editedon); + + + chrome.browserAction.setBadgeText ({ + text: request.published, + tabId: sender.tab.id + }); + chrome.browserAction.setBadgeBackgroundColor({ + color: badge_color, + tabId: sender.tab.id + }); + chrome.browserAction.setTitle({ + title: tooltip.join('\n'), + tabId: sender.tab.id + }); + }); \ No newline at end of file diff --git a/src/content.js b/src/content.js index 3823056..e6fe39a 100644 --- a/src/content.js +++ b/src/content.js @@ -1,14 +1,29 @@ -/* Listen for messages and get modResource meta tag*/ +var META_NAME = 'application-name'; +var APP_NAME = 'modxchromemanager'; + +var meta = document.querySelector("meta[name='" + META_NAME + "'][content='" + APP_NAME + "']"); +var docid, + published, + editedon, + editedby; + +if (meta) { + docid = meta.getAttribute('data-id'); + published = meta.getAttribute('data-published'), + editedon = meta.getAttribute('data-editedon'), + editedby = meta.getAttribute('data-editedby'); + + chrome.runtime.sendMessage({ + "docid": docid, + "published": published, + "editedon": editedon, + "editedby": editedby, + }); +} + +/* Listen for messages and get docid meta tag*/ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { - if (msg.command && (msg.command == "getResource")) { - - function getMetaContentByName(name,content){ - var content = (content==null)?'content':content; - var meta = document.querySelector("meta[name='" + name + "']"); - if (meta) { - return meta.getAttribute(content); - } + if (msg.command && (msg.command == "getManager")) { + sendResponse(docid); } - sendResponse(getMetaContentByName('application-name','data-id')); - } }); \ No newline at end of file