Search for specific content on a webpage in Firefox using JavaScript

Currently, I am working on developing an extension that will provide grep functionality in Firefox. This feature would be extremely useful for filtering results and examining specific logging levels (INFO, WARN, ERROR) from log files accessed through the browser at my workplace.

I have successfully set up the extension boilerplate but now I need guidance on the necessary JavaScript code. Specifically, I am looking for a function:

function grepPage(regex){
...
}

This function should apply the provided regex to each line of text in a loaded file within Firefox and then display only the lines that match the regex.

Although I could spend a significant amount of time trying to figure this out on my own, I believe there might be simpler solutions available. Any assistance or hints in the right direction would be greatly appreciated.

Thank you in advance,

Ben

Answer №1

There are two approaches you can take with this.

Firstly, instead of starting from scratch, consider utilizing existing resources like: http://api.jquery.com/jQuery.grep/

Alternatively, you could create a simple function without relying on jQuery or other frameworks.

function findMatches(regex) {

  var lines = document.getElementsByTagName('body')[0].innerHTML.split("\n");
  var matches = [];

  if (!regex.match(/^\//)) { 
    regex = '/' + regex + '/'; 
  }

  for (var i = 0; i < lines.length; i++) {
    if (regex.test(lines[i])) { 
      matches.push(lines[i]); 
    }
  }

  // The 'matches' array now holds all the matching lines for further processing.

}

Please note that this code has not been tested, but it is expected to function correctly. :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to determine the size of the URLs for images stored in an array using JavaScript?

I am working on a project where I need to surround an image with a specific sized 'div' based on the image's dimensions. The images that will be used are stored in an array and I need to extract the height and width of each image before disp ...

Aggregate information from an array containing multiple nested arrays

With regards to marking this as answered by another question, please take note that this is not a flat array but an array of arrays. Additionally, the numbers provided are just examples for visual representation. I am attempting to iterate through an arra ...

Experiencing the issue of receiving unexpected commas following a div

Here is the code written in TypeScript for creating an HTML table that displays items from nested objects. The code is functional, but there seems to be an issue with extra commas being printed which are not part of any specific line being executed. im ...

Leveraging res.render {objects} within client-side rendering

I admit that I may not be the best, which is why I'm seeking help here. However: con.connect(); con.query('SELECT name, lname, rank FROM players LIMIT 1', function(err,rows, fields){ if(err) throw err; result = rows; resultstring ...

Is there a way to stop a for-in loop within a nested forEach function in JavaScript?

I am facing a situation with nested loops for (var key in params) { if (Array.isArray(params[key])) { params[key].every(function(item) { let value = something(item.start, item.end); if (value === item.start || value == item.end) { ...

Uniform Height for Several Selectors

I came across a script on Codepen created by RogerHN and decided to customize it for a project I'm currently working on: https://codepen.io/RogerHN/pen/YNrpVa The modification I made involved changing the selector: var matchHeight = function ...

Package tracking static document within javascript module

I'm currently working on a project in parcel that involves three.js. It appears that I am having trouble loading a 3D model due to an incorrect path issue. My model is located in src/assets/models/mymodel.obj and I am attempting to use it within the ...

What is the CoffeeScript alternative for () => { return test() }?

Currently, I am attempting to write this code in CoffeeScript and finding myself at a standstill... this.helpers({ events: () => { return Events.find({}); } }); ...

Switching images with a click using jQuery

Is there a way to swap out banners (images) when a user clicks on a link? Here are the links: <li><a href="#" id="button1">1</a></li> <li><a href="#" id="button2">2</a></li> The image in question: <img ...

Retrieving string array elements within a Vue.js v-for iteration

Trying to extract string values from an array within a function. The component declared in the template looks like this: <video-preview v-for="video in playlist" vid-id="how-to-retrieve-string -value-from-the-current-iteration-in-playlist"></vi ...

Instructions on how to configure an extension in geckodriver using Selenium with Python

Recently, I attempted to load the Zenmate VPN extension in the Firefox driver using the following code. Despite successfully opening the driver, the extension failed to load. from selenium import webdriver profile = webdriver.FirefoxProfile() profile.add_ ...

I encountered an error while trying to add a document to Firestore using the 'add' method in Vue.js. Can someone provide guidance on how to

Whenever the function is triggered (on click), I aim to include a new document. Although it functions with .set(), my goal is for a new document to be created each time the form is submitted. The current error code I am encountering is: https://i.sstatic ...

Tips for improving modularity in my frontend JavaScript code?

I'm currently developing a unique Node.js / Express application that visually represents text notes as a network to offer users a clear summary of the connections between different notes. The project heavily relies on frontend functionalities, requir ...

Dynamically binding image URLs in VUEJS

Below is the JSON data containing button names and their corresponding image URLs: buttonDetails= [ { "name": "button1", "images": [{ "url": "https://localhost:8080/asset/d304904a-1bbd-11e6-90b9-55ea1f18bb ...

What is the best way to retrieve data from a fetch request within a GET function?

It seems like a simple issue, but I'm struggling to retrieve information from my "body" when utilizing the get method. I've experimented with various approaches to extract the data, but nothing seems to work. Any guidance would be greatly appreci ...

Modify the colors of multiple texts using a concise code block of fewer than 20 lines

I have 8 paragraphs and I want to toggle the class 'one-active' when clicking on each of them, while removing the active class from the others. I currently have the code working for two paragraphs, but it's too lengthy for all eight. How can ...

Error encountered with Ajax client-side framework while executing HTML code

When I run my project from Visual Studio using an aspx page that utilizes ajax modal popup extender, everything works fine with IE and Firefox as the default browsers. However, when I create an HTML file containing the code and open it by double-clicking, ...

How can we efficiently execute text searches on a large scale by utilizing static index files that are easily accessible online

Looking for a lightweight, yet scalable solution for implementing a full text search index in JavaScript using static files accessible via HTTP? Seeking to make about 100k documents searchable online without breaking the bank on hosting costs like Elastics ...

How to correct header alignment in HTML for Google Table

Utilizing the google visualization table to generate a html table, I have successfully fixed the top section of the html using the stuckpart div. This ensures that regardless of how I scroll, the button remains in place. However, I now aim to fix the table ...

Can elements be prevented from resizing in relation to another element without using position:absolute?

Is there a way to prevent elements in a <div> from moving when an adjacent element is resized, without using position:absolute? Any help would be greatly appreciated. Thank you in advance! ...