I am creating a Chrome extension. I am trying to connect my application to every page in the extension and the page that the user is viewing in the browser. I need to access dom from an extension and then update it.
manifest.json popup.html popup.js background.js content.js
and the current page that the user is viewing.
My goal is to load the page in order to change the dom and show the user a new version of the page before she sees it. in popup.js users are allowed to enter keywords in a popup window. Keywords are stored in localStorage , and while they browse the web, the keywords are censored from their view, causing the parent div to hide the keywords if they are found on any pages that they browse.
I need help getting each page to share data, and I think the way I hide parent divs in popup.js will not work. I am confused about how to perform an action on dom from the front.
Send dom to background.js Find the keywords on the page and change their parent divs to hidden ones. click home back to the watch page.
I think this line says that if I map any URL, then run my application, but I'm not sure.
"matches": ["*://*/*"],
My manifest is .json
{ "name": "Wuno Zensoring", "version" : "1.0", "permissions": [ "activeTab", "tabs", "storage" ], "description": "This extension will search the document file for keywords and hide their parent div.", "icons": { "19": "icon19.png", "38": "icon38.png", "48": "icon48.png", "128": "icon128.png" }, "background": { "persistent": false, "scripts": ["jquery-1.11.3.min.js","background.js"] }, "content_scripts": [{ "matches": ["*://*/*"], "js": ["content.js"], "run_at": "document_end", "all_frames": true }], "web_accessible_resources": [ "popup.js", "content.js" ], "browser_action": { "default_icon": "icon.png128", "default_popup": "popup.html", "default_icon": { "19": "icon19.png", "38": "icon38.png", "48": "icon48.png", "128": "icon128.png" } }, "manifest_version": 2 }
popup.html
<!doctype html> <html> <head> <title>Wuno Zensorship</title> <script src="jquery-1.11.3.min.js"></script> <script src="popup.js"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <img src="icon48.png"> <section> <form id="form" action="#" method="POST"> <input id="description" name="description" type="text" /> <input id="add" type="submit" value="Add" /> <button id="clearChecked">Clear Checked Items</button> <button id="clear">Clear All</button> </form> <div id="alert"></div> <ul id="keyWords"></ul> </body> </html>
popup.js
$(document).ready(function () { localArray = []; if (!localStorage.keyWords) { localStorage.setItem('keyWords', JSON.stringify(localArray)); } loadKeyWords(); function loadKeyWords() { $('#keyWords').html(''); localArray = JSON.parse(localStorage.getItem('keyWords')); for(var i = 0; i < localArray.length; i++) { $('#keyWords').prepend('<li><input class="check" name="check" type="checkbox">'+localArray[i]+'</li>'); } } $('#add').click( function() { var Description = $('#description').val(); if($("#description").val() === '') { $('#alert').html("<strong>Warning!</strong> You left the to-do empty"); $('#alert').fadeIn().delay(1000).fadeOut(); return false; } $('#form')[0].reset(); var keyWords = $('#keyWords').html(); localArray.push(Description); localStorage.setItem('keyWords', JSON.stringify(localArray)); loadKeyWords(); return false; }); $('#clear').click( function() { window.localStorage.clear(); location.reload(); return false; }); $('#clearChecked').click(function() { currentArray = []; $('.check').each(function() { var $curr = $(this); if (!$curr.is(':checked')) { var value = $curr.parent().text(); currentArray.push(value); localStorage.setItem('keyWords', JSON.stringify(currentArray)); loadKeyWords(); } else { $curr.parent().remove(); } }); });
background.js
chrome.runtime.onMessage.addListener(function (msg, sender) {
content.js
// Inform the background page that // this tab should have a page-action chrome.runtime.sendMessage({ from: 'content', subject: 'showPageAction' }); // Listen for messages from the popup chrome.runtime.onMessage.addListener(function (msg, sender, response) { // First, validate the message structure if ((msg.from === 'popup') && (msg.subject === 'DOMInfo')) { // Collect the necessary data // (For your specific requirements `document.querySelectorAll(...)` // should be equivalent to jquery `$(...)`) var domInfo = { total: document.querySelectorAll('*').length, inputs: document.querySelectorAll('input').length, buttons: document.querySelectorAll('button').length }; // Directly respond to the sender (popup), // through the specified callback */ response(domInfo); } });