Search code examples
javascriptjqueryhtmlbookmarklet

Bookmarklet issue


I'm trying to get this bookmarklet running but I get this error (in Chrome):

Uncaught SyntaxError: Unexpected identifier

I don't get what's wrong with my JavaScript code in the href. I can't put it in a separate file; I need to get this running in the href.

    <a href="javascript:(function(){
      if (!($ = window.jQuery)) { 
        script = document.createElement( 'script' );
        script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; 
        script.onload=releasetheScript;
        document.body.appendChild(script);
      } 
      else {
        releasetheScript();
      }
    
      function releasetheScript() {
        
        regex = ['ceo', 'fondateur', 'cofondateur', 'cto', 'cfo', 'daf', 'startup', 'entrepreneur', 'office manager', 'fintech', 'freelance', 'tech', 'founder', 'neobanque', 'banking', 'comptable', 'incubateur', 'coworking', 'PME', 'VC', 'accélérateur']
        banned = ['|(?!', 'sex', 'porn']
        target = regex.join('|') + banned.join('|') + ')';
        interval = 10000
        a = setInterval(function () {
          window.scrollTo(0,document.body.scrollHeight);
          var fields = $('.ProfileCard-userFields');
          for (var i = 0; i < fields.length; i++) {
            var p = fields[i].getElementsByClassName('ProfileCard-bio');
            if (p[0].textContent.length > 1) {
              if (p[0].textContent.match(new RegExp(target), 'g') !== null) {
                console.log(p[0].textContent, 'text matching')
              }
            }
          }
        }, interval);
      }
    })()">Bookmarklet</a>


Solution

  • Add semicolons at the ends of all the statements, ASI isn't filling them in where you need them. I'm not going to bother trying to figure out why, just get in the habit of always using them.

        <a href="javascript:(function(){
          if (!($ = window.jQuery)) { 
            script = document.createElement( 'script' );
            script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; 
            script.onload=releasetheScript;
            document.body.appendChild(script);
          } 
          else {
            releasetheScript();
          }
        
          function releasetheScript() {
            
            regex = ['ceo', 'fondateur', 'cofondateur', 'cto', 'cfo', 'daf', 'startup', 'entrepreneur', 'office manager', 'fintech', 'freelance', 'tech', 'founder', 'neobanque', 'banking', 'comptable', 'incubateur', 'coworking', 'PME', 'VC', 'accélérateur'];
            banned = ['|(?!', 'sex', 'porn'];
            target = regex.join('|') + banned.join('|') + ')';
            interval = 10000;
            a = setInterval(function () {
              window.scrollTo(0,document.body.scrollHeight);
              var fields = $('.ProfileCard-userFields');
              for (var i = 0; i < fields.length; i++) {
                var p = fields[i].getElementsByClassName('ProfileCard-bio');
                if (p[0].textContent.length > 1) {
                  if (p[0].textContent.match(new RegExp(target), 'g') !== null) {
                    console.log(p[0].textContent, 'text matching')
                  }
                }
              }
            }, interval);
          }
        })()">Bookmarklet</a>