Nizar AYARI
Nizar AYARI

Reputation: 203

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>

Upvotes: 3

Views: 280

Answers (2)

JLRishe
JLRishe

Reputation: 101652

Your bookmarklet will be run as a single line of script, so semicolons are not optional. Your script is failing because you're missing semicolons.

Two side notes:

  • Your code is using implicit globals for seemingly no reason. Use var to declare variables.
  • You're better off starting the script URL with // rather than http://, to ensure that the script is loaded regardless of the scheme of the local page.

<a href="javascript:(function(){
  if (!window.$ || window.$ !== window.jQuery) { 
    var script = document.createElement( 'script' );
    script.src = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; 
    script.onload=releasetheScript;
    document.body.appendChild(script);
  } 
  else {
    releasetheScript();
  }

  function releasetheScript() {
    /* v--- need a semicolon on this line */
    var 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']; 
    var banned = ['|(?!', 'sex', 'porn'];  /* <-- here  */
    var target = regex.join('|') + banned.join('|') + ')';
    var interval = 10000;    /* <-- here  */
    var 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');  /* <-- and here  */
          }
        }
      }
    }, interval);
  }
})()">Bookmarklet</a>

Upvotes: 3

Barmar
Barmar

Reputation: 780655

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>

Upvotes: 1

Related Questions