Grep page in firefox using javascript

I am writing an extension to provide grep'ing functionality in Firefox. At my workplace, we access all the log files using a browser, and the grep function is ideal for filtering results, looking only at certain levels of logging (INFO, WARN, ERROR), etc.

Sets the extension pattern.

I wonder if I can get hints for the right javascript. Am after function:

function grepPage(regex){ ... } 

which would apply a regular expression to each line of the loaded text file in firefox and modify the loaded text file to display only lines that match.

This is the type of thing that I could spend years trying to figure out when I'm sure there will be easier ways to do this.

Any help would be greatly appreciated.

Cheers, Ben

+7
source share
1 answer

Two ways to look at this.

First, you could avoid reinventing the wheel: http://api.jquery.com/jQuery.grep/

Two, you can quickly deploy a function (this example does not require jQuery or other libraries).

 function grepPage(regex) { var lines = document.getElementByTagName('body').innerHTML.split("\n"); var matches = new Array(); // Check if the regex is surrounded by slashes by checking for a leading slash if ( ! regex.match(/^\//) ) { regex = '/' + regex + '/'; } for (var i = 0; i < lines.length; i++) { if ( regex.test( lines[i] ) ) { matches.push(lines[i]); } } // Now the 'matches' array contains all your matches, do as you will with it. } 

Warning, untested, but it should work. :)

+5
source

All Articles