Bit of coding help - var el =

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,144
Reaction score
10,489
I know the solution to this is super simple, but I'm no coder, and I'm having a brain freeze right now.

I have a Firefox extension which closes the tab if a certain ID appears on the page.

This code:

Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){
  var el = sel('#text')

  if( el.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")

See where it says "var el = sel('#text')"?

So when "ID=text" appears on the page, the page is closed.

But I also want it to close the page when the "ID=change" appears. I tried this:

Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){
  var el = sel('#text')
  var el = sel('#change')

  if( el.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")

But this only closes the page when "ID=change" appears, and it stops working for "ID=text."

So, what's the right arrangement of code to make it work for both? Thanks :)
 
I tried 2 things that came to mind, both fail :(

Any help?


Edit: I tried a 3rd thing that came to mind, still not working. WTF.
 
Last edited:
Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){
  var el = sel('#text');
  var pl = sel('#change');

  if( el.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }
  else if( pl.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }
  else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")

maybe this will work, just tried a quick workaround
 
Last edited:
Try something like this.
JavaScript:
const ids = ["#text", "#change"];
for (const id in ids) {
  const el = sel(id);
  if( el.classList.contains('hidden') == false ){
    //some logic
    
  }
}
 
Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){
  var el = sel('#text')
  var pl = sel('#change')

  if( el.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }
  else if( pl.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }
  else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")

maybe this will work, just tried a quick workaround

Unfortunately, doesn't work.

It works for "var el = sel('#text')"

It doesn't work for "var pl = sel('#change')"
 
Try something like this.
JavaScript:
const ids = ["#text", "#change"];
for (const id in ids) {
  const el = sel(id);
  if( el.classList.contains('hidden') == false ){
    //some logic
   
  }
}

I tried it. Not sure if I did it how you meant to do it, but, here, I did this:

Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){

const ids = ["#text", "#change"];
for (const id in ids) {
  const el = sel(id);

  if( el.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")

I probably didn't do it the way you meant to do it, but there you go :)
 
Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){
  var el = sel('#text')
  var el1 = sel('#change')

  if( el.classList.contains('hidden') == false || el1.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")
 
Code:
function sel(sel, el) {    return (el || document).querySelector(sel); }
function selAll(sel, el) { return (el || document).querySelectorAll(sel); }

function closeTab(){ browser.runtime.sendMessage("close") }

function scan(){
  var el = sel('#text')
  var el1 = sel('#change')

  if( el.classList.contains('hidden') == false || el1.classList.contains('hidden') == false ){// means visible
    setTimeout(closeTab, 5*1000 )
    console.log("closing script ", "closing in5 secs")
  }else{
    setTimeout(scan, 2*1000 )
  }
}
setTimeout(scan, 2*1000 )
console.log("closing script IN...")

Works for one ID, but not for the other.
 
Okay, seeing as I got a number of replies that didn't solve the problem, I had to take ugly extreme non-elegant measures.

I created 2 separate extensions, one for each ID, added both to Firefox.

I tried it and it worked on both IDs.

Problem solved, I guess :D

Thanks all
 
Okay, seeing as I got a number of replies that didn't solve the problem, I had to take ugly extreme non-elegant measures.

I created 2 separate extensions, one for each ID, added both to Firefox.

I tried it and it worked on both IDs.

Problem solved, I guess :D

Thanks all
What u up to lol
Gd luck well done/
 
@Scorpion Ghost here is a very verbose solutions for everyone to understand easily

JavaScript:
const configuration = {
  targetIds: ['text', 'change'], // all ids you wanna watch
  closeBrowserAfter: 5, // time in seconds before closing browser
  rescanAfter: 2, // time in seconds before scanning again
  startScriptAfter: 2 // time in seconds before starting the first time
}

// this function retrieve an HTML element by an ID
const selectElementById = id => document.getElementById(id)

// this function check wether an element does not have a specific class
const elementDoesntHaveClass = clazz => element => !element.classList.contains(clazz)

// this function seems to close the browser
const closeBrowser = () => browser.runtime.sendMessage("close")

// this function scan the page and close the browser if an element with one of
// the specific ids also has the class `hidden`
const scanPage = () => {
  // elements is an array of found html elements that have at least one of configured ids
  const elements = configuration.targetIds.map(selectElementById).filter(x => x)

  // we check if at least one of the element is visible
  if(elements.some(elementDoesntHaveClass('hidden'))) {
    console.log("closing script |", `closing in ${configuration.closeBrowserAfter} secs`)
    
    // let's close the browser in X seconds
    setTimeout(closeBrowser, configuration.closeBrowserAfter * 1000)
    return
  }

  // if no elements are found or at least not visible then we keep scanning
  setTimeout(scanPage, configuration.rescanAfter * 1000)
}

// start the script after X seconds
setTimeout(scanPage, configuration.startScriptAfter * 1000)
 
Back
Top