0

I am new to Chrome extensions so I am having trouble knowing why I am not getting my expect results.

I have a script that allows you to add keywords. I want my program to keep track of the keywords and hide the parent div of the keywords. Essentially it would filter out stuff you do not want to see. Like, Justin Bieber or Kim Kardashian.

I can add the keywords with no problem but I cannot get the content to be hidden.

I think the problem is I may not be targeting the dom correctly of the page I am viewing.

Maybe missing a permission?

Could someone please tell me how to do this with an extension?

keywords.js

$(document).ready(function () {

$('#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;
   }
   $('#keyWords').prepend("<li><input id='check' name='check' type='checkbox'/>" + Description + "</li>");
   $('#form')[0].reset();
   var keyWords = $('#keyWords').html();
   localStorage.setItem('keyWords', keyWords);
   return false;
});

if(localStorage.getItem('keyWords')) {
$('#keyWords').html(localStorage.getItem('keyWords'));
}

$('#clear').click( function() {
window.localStorage.clear();
location.reload();
return false;
});

}); // End of document ready function


$(document).ready(function() {
  $("div p:contains(localStorage.getItem('keyWords')).parent('div').hide()");
}); //end of document ready function

This is my Manifest.json

{

 "name": "Wuno Zensoring",
  "version" : "1.0",
   "permissions": [
   "activeTab",
   "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"  
  },    
  "browser_action": {
    "default_icon": "icon.png128",
    "default_popup": "keyform.html",
    "background_page": "keyform.html",
    "default_icon": {                   
      "19": "icon19.png",
      "38": "icon38.png",
      "48": "icon48.png",
      "128": "icon128.png"        
  }
  },
     "manifest_version": 2
}

My html file keyform.html

<!doctype html>
<html>
  <head>
    <title>Wuno Webpage Analyzer</title>
    <script src="jquery-1.11.3.min.js"></script>
        <script src="keywords.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="clear">Clear All</button>
</form>
<div id="alert"></div>
<ul id="keyWords"></ul>
</body>
</html>
wuno
  • 9,547
  • 19
  • 96
  • 180
  • What does the markup you are trying to manipulate look like? – Adam Konieska Dec 23 '15 at 19:04
  • You've also got a misplaced quote in your `$("div p:contains(localStorage.getItem('keyWords')).parent('div').hide()");`You probably want `$("div p:contains("+localStorage.getItem('keyWords')+")").parent('div').hide();` – Adam Konieska Dec 23 '15 at 19:07
  • Thank you for point that out. But it still does not hide anything.. Basically if one of the keywords being held in local storage I want the whole parent div to become hidden. Is this a logical way to handle this and any clue how to target the document from my extension? – wuno Dec 23 '15 at 19:13
  • What is being stored in keywords? If its a single string it will work as you've written (see this JS fiddle example: https://jsfiddle.net/3u3fctth/) but for multiple strings you're going to need to iterate over them individually. – Adam Konieska Dec 23 '15 at 19:17
  • Well It is a list of strings. I am allowing users to store as many keywords as they want. So the app will hide everything they dont want to see. So I need a for loop? – wuno Dec 23 '15 at 19:19
  • Correct. See this JS fiddle example with a loop: https://jsfiddle.net/3u3fctth/1/ – Adam Konieska Dec 23 '15 at 19:20
  • this should work then? for(var i = 0; i < multiStr.length; i++) { $("div p:contains("+localStorage.getItem('keyWords')+")").parent('div').hide(); } – wuno Dec 23 '15 at 19:23
  • No, that wont work. It depends on how the data is structured in keyWords. Is it an array, an object, a comma separated list, etc? – Adam Konieska Dec 23 '15 at 19:24
  • I am using local storage setItem and getItem I am understanding it as it only takes strings, but I assumed it was adding them in as an array, Does this make sense to you? http://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage – wuno Dec 23 '15 at 19:27
  • In my code I am using the setItem and getItem function with local.storage but when I use the methods I print the list on the screen in the order they were entered like this, $('#keyWords').prepend("
  • " + Description + "
  • "); $('#form')[0].reset(); var keyWords = $('#keyWords').html(); localStorage.setItem('keyWords', keyWords); return false; – wuno Dec 23 '15 at 19:30
  • It doesn't look like you are creating them as an array. In fact, you're storing all the HTML thats in #keyWords in localstorage. Double check that, and the JSON in that SO question you linked would help you get them into an array. – Adam Konieska Dec 23 '15 at 19:34
  • But I need them in local storage so I can save them each time the user closes browser moves around etc. – wuno Dec 23 '15 at 19:38
  • Or are you saying keep in local storage, push to array uses the array in the loop the way you suggested and my app will work? – wuno Dec 23 '15 at 19:38