diff --git a/manifest.json b/manifest.json index 7076d2b..6b17d4a 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Custom Elements Locator", "description": "This extension will find any custom element on a page.", - "version": "1.0", + "version": "1.1", "icons": { "16": "meta_assets/icon16.png", "48": "meta_assets/icon48.png", @@ -18,8 +18,11 @@ "js": ["dist/content_script.js"] } ], + "background": { + "scripts": ["dist/background_script.js"] + }, "permissions": [ "activeTab", "background" ] -} \ No newline at end of file +} diff --git a/meta_assets/icon_without.png b/meta_assets/icon_without.png new file mode 100644 index 0000000..8c2c5b1 Binary files /dev/null and b/meta_assets/icon_without.png differ diff --git a/package.json b/package.json index a08b27f..38daf8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "custom-elements-locator", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "content_script.js", "scripts": { diff --git a/rollup.config.js b/rollup.config.js index 7fb0063..46ea1a9 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -27,4 +27,14 @@ export default [{ plugins: [ resolve() ] -}]; \ No newline at end of file +}, +{ + input: './src/background_script.js', + output: { + dir: 'dist', + format: 'iife', + }, + plugins: [ + resolve() + ] +}]; diff --git a/src/background_script.js b/src/background_script.js new file mode 100644 index 0000000..aa7d58c --- /dev/null +++ b/src/background_script.js @@ -0,0 +1,31 @@ +const metaAssets = '../meta_assets/'; +var selectedId = -1; + +function getIconPath(response) { + const suffix = response.length ? '' : '_without'; + return `${metaAssets}icon${suffix}.png`; +} + +function updateIcon() { + chrome.tabs.sendMessage(selectedId, {msg: "findAll"}, (response = []) => { + chrome.browserAction.setIcon({ + path: getIconPath(response), + tabId: selectedId + }); + }); +} + +chrome.tabs.onUpdated.addListener(function(tabId, props) { + if (props.status == "complete" && tabId == selectedId) + updateIcon(); +}); + +chrome.tabs.onSelectionChanged.addListener(function(tabId, props) { + selectedId = tabId; + updateIcon(); +}); + +chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + selectedId = tabs[0].id; + updateIcon(); +}); diff --git a/src/content_script.js b/src/content_script.js index ea03f68..f6c606b 100644 --- a/src/content_script.js +++ b/src/content_script.js @@ -16,20 +16,33 @@ function findAllCustomElements(nodes) { if (isCustomElement(el) && el.localName !== 'style' && !allCustomElements.find(ce => ce === el.localName)) { allCustomElements.push(el.localName); } - + if (el.shadowRoot) { findAllCustomElements(el.shadowRoot.querySelectorAll('*')); } } } +chrome.runtime.onConnect.addListener(function(port) { + port.onMessage.addListener(function(msg) { + port.postMessage({counter: msg.counter+1}); + }); +}); + chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - if(request.msg === "init") { + + if(request.msg === "findAll") { allCustomElements = []; findAllCustomElements(document.querySelectorAll('*')); sendResponse(allCustomElements) } + if(request.msg === "init") { + // Custom elements are already found. + // Return cached ones. + sendResponse(allCustomElements); + } + if(request.msg === "highlight") { if(lastElements.length > 0 ) lastElements.forEach(el => el.style.border = 'none'); const elements = querySelectorAllDeep(request.elementName) @@ -70,7 +83,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { elements[index].scrollIntoView(); - window.scrollBy(0, -200); + window.scrollBy(0, -200); elements[index].setAttribute( 'style', 'border: dashed 3px blue !important;' ); console.group('Custom Element Locator');