javascriptgetelementsbytagname

Find every src value in a webpage that contains certain text javascript


I want to find and "console.log" every src value in an HTML page that has a certain text; for example (dam/etc/vsvs). Is it possible to do it with javascript and in the console, as I'm not able to reach to source code of the page? I'm not a developer just trying to find the images I want and list them.

Any help will be appreciated.

Thanks in advance.


Solution

  • As far as I understand, you have an HTML page and you need to grab every src value which meets some expectations (i.e. starts with smth, includes smth).

    So, if you are able to run a script on the page then you can recursively go node by node and check if it has an src attribute.

    const findSrc = (node) => {
      const attributesToCheck = ['src', 'srcset', 'data-srcset']
    
      const srcs = attributesToCheck.map(name => {
        const attr = node.getAttribute(name)
    
        // here is a condition for your match
        return attr?.startsWith("/etc") ? attr : false
      }).filter(Boolean)
    
      if (node.children) {
        return srcs.concat(Array.from(node.children).flatMap(findSrc));
      }
    
      return srcs;
    };
    
    console.log(findSrc(document.body));