-5

I want to make chrome extension to remove some advertisments from webpages.

I think I have to learn to remove lines of text from a longer text to do that:

For example, given this text:

This is text that contains some string bla This is text that contains
some string bla This is text DELETEME some string bla This is text
that contains some string bla This is text that contains some string
bla

how can I remove the whole line of the text that contains the string DELETEME?

as text will be used document.body.InnerHTML; I want to take out some garbage :}

bjelli
  • 9,752
  • 4
  • 35
  • 50
user2200585
  • 283
  • 5
  • 15
  • You should show some effort, just don't say "I don't know" – Siva Charan May 05 '13 at 14:29
  • 1
    your example text is all on one line? or is it supposed to be four lines? – bjelli May 05 '13 at 14:30
  • as text will be used document.body.InnerHTML; I want to take out some garbaje :} – user2200585 May 05 '13 at 14:34
  • Googling "javascript delete line" found: http://stackoverflow.com/questions/2528076/delete-a-line-of-text-in-javascript – Bryan Ash May 05 '13 at 14:36
  • 1
    @user2200585 On StackOverflow you are expected to be familiar with the subject you're asking about. This is not a copy-paste forum. – Boaz May 05 '13 at 14:43
  • @user2200585 stackoverflow will not be useful to you at your current state of knowledge/ignorance. You should try learning the basics of Javascript from a beginners book like "Head First Javascript". Then you will have the basic vocabulary to ask meaninful questions and understand the answers here. – bjelli May 05 '13 at 15:01

2 Answers2

2

You could split the text into lines:

var lines = text.split('\n');

Then filter out the ones that contained that string:

lines.filter(function(line) {
    return line.indexOf('DELETEME') === -1;
})

Then join that back together:

text = lines.filter(function(line) {
    return line.indexOf('DELETEME') === -1;
}).join('\n');
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • if I want to put a document.body.InnerHTML; in text, I should do this yes? -> var text = document.body.InnerHTML; ? – user2200585 May 05 '13 at 14:38
  • @user2200585: Sure, or just use `lines = document.body.innerHTML.split('\n');`. Why do you want to do this to a page, out of curiosity? :) – Ry- May 05 '13 at 14:39
  • I'll make chrome extension to remove some advertisments.. :) from website – user2200585 May 05 '13 at 14:46
  • @user2200585: This isn’t a good way to do it; use the DOM. Also, is this just a hobby project to learn JavaScript, or something serious? – Ry- May 05 '13 at 14:52
  • This is a hobby project, I do not earn anything from this project, just making my life easier :)) (look how much dislikes I have :D lol) – user2200585 May 05 '13 at 15:02
  • @user2200585: Well, have you tried AdBlock Plus? Replacing the innerHTML of an entire page is probably going to break all of said page’s JavaScript… – Ry- May 05 '13 at 15:03
  • no, i have to block video ad, first video ad starts, then video, i have to cut video ad out of src.. – user2200585 May 05 '13 at 15:15
2

For your project (creating a chrome extension to remove ads) you do not actually need to manipulate text, you need to manipulate HTML code, or more specificall: the DOM, the document object model that represents the HTML code in your browser. You can do this quite conveniently with jQuery.

Here's how you would delete ALL li's containting DELETEME:

$('li:contains(DELETEME)').remove();

here's a fiddle: http://jsfiddle.net/bjelline/RrCGw/

The best way to try this out is to open den developer tools in your browser and type the commands into the console, then you will see the effect immediately.

For example: google for "learn javascript" - you'll probably see an ad. Look at the HTML source code to finde out that the id is inside a div with id "tads". Now open the console and type in

$('#tads').remove();

And the ads will disappear.


You specifically asked about manipulating text. When manipulating text it's a good idea to learn about regular expressions - not just for JavaScript, you can use them in many programming languages.

If your whole text is stored in the variable string, you could do this:

string = string.replace(/.*DELETEME.*/, "XXXX");

to replace the line with XXXX. Just use an empty string as a replacement to empty it completely:

string = string.replace(/.*DELETEME.*/, "");

The ".*" stands for "any character, repeated as often as necessary", which matches the text before and after DELETEME. This regular expression only works on one line, so text on other lines is not changed.

See http://jsfiddle.net/bjelline/Wc7ve/ for a working example.

But, as stated above: this is not the right tool for your project.

bjelli
  • 9,752
  • 4
  • 35
  • 50