Search a page using javascript

I have an html page and I want to find some data on it, but the main problem is that the page is created on the server, and I want to write javascript code on my local computer and run it. So, how can I write and run javascript code on the local computer so that it can find text or get an element by id / class?

Please note: this is important: only pure javascript, no libraries like jQuery etc.!

Thank.

+3
source share
2 answers

Updated answer :

At first I didn’t understand that you want to call a web page that you don’t control, and then use JavaScript in your browser to interact with it.

- , : ? : :

  • . , F12 Ctrl + Shift + I. "", JavaScript , .

    , . (, /home/tjc/foo.js), , , , script ( ), :

    document.documentElement.appendChild(document.createElement('script')).src = "file:///home/tjc/foo.js";
    
  • script , , bookmarklet. javascript:, http: .. . . , JavaScript URL-, Bookmarklet Crunchinator .


:

... id/class...

:

  • , :

    • , , , innerHTML on document.body. innerHTML - ; HTML DOM , ( ). , ; " " . , . , , , , , "", ( " " ) (<table>...).

      I'm innerHTML: | - . .

      (function() {
      
        var pageText = document.body.innerHTML;
        display('Count of "I\'m" on the page: ' +
                pageText.match(/I'm/g).length);
      
        function display(msg) {
          var p = document.createElement('p');
          p.innerHTML = String(msg);
          document.body.appendChild(p);
        }
      
      })();
      
    • , , , , Text, . ( - walk): | - . .

      (function() {
        var matches = [], index;
      
        walk(matches, document.body, "");
      
        function walk(matches, node, path) {
          var child;
      
          switch (node.nodeType) {
            case 1: // Element
              for (child = node.firstChild; child; child = child.nextSibling) {
                walk(matches, child, path + "/" + node.tagName);
              }
              break;
            case 3: // Text
              if (node.nodeValue.indexOf("I'm") !== -1 ) {
                matches.push("Found it at " + path);
              }
              break;
          }
        }
      
        display("Matches found (" + matches.length + "):");
        for (index = 0; index < matches.length; ++index) {
          display(matches[index]);
        }
      
        function display(msg) {
          var p = document.createElement('p');
          p.innerHTML = String(msg);
          document.body.appendChild(p);
        }
      
      })();
      
  • id, document.getElementById.

  • class, getElementsByClassName querySelectorAll.

: JSBin, JavaScript, , "source" end HTML, ( </body>), . .

:

+4

imacros, :

 var reportDataTable = window.content.document.getElementById("yoursid");
 if (reportDataTable == null)
 {
    iimPlay("mac1.iim");
 }
 else
 {
    iimDisplay("stop!");
 }

mac1.iim - , ,

window.content.document.getElementById( "yoursid" );

+1

All Articles