0

I am creating a chrome javascript bookmarklet with reference to:

this question

My code:

javascript:(function NoOverrideAction(t){this.currentWindow=t,this.urlMatchPattern=/https:\/\/.*visual\.force\.com\/apex\/.*/,this.urlMatchPattern1=/https:\/\/.*visualforce\.com\/apex\/.*/,null!=this.currentWindow.location.toString().match(this.urlMatchPattern)||null!=this.currentWindow.location.toString().match(this.urlMatchPattern1)?this.isPageValid=!0:this.isPageValid=!1,this.recordId} NoOverrideAction.prototype={getId:function(){this.currentWindow.location.search.substr(1).split("&").forEach(function(t){var i=t.split("=");"id"==i[0]&&(this.recordId=i[1])},this)},run:function(){this.getId(),console.log(this),this.isPageValid&&void 0!==this.recordId&&(this.currentWindow.location.href="https://"+this.currentWindow.location.hostname+"/"+this.recordId+"?nooverride=1")}};var noAction=new NoOverrideAction(window);noAction.run();)();

However, I get following error:

Uncaught SyntaxError: Unexpected identifier

This works well in console but not as a bookmarklet.

I am trying to verify the URL of my current page and replace the URL

pmehta28
  • 58
  • 4

1 Answers1

2

Part I.

Several reasons why js-code might work in console, but not work as a bookmarklet.

1 - You don't use the established way of making bookmarklets, i.e. IIFE-functions.

javascript:(function(){
alert('hi');
/*code here*/
})();

2 - If you do use this structure, there might be the problem with comments.

Use /*multi-line comment*/ instead of //single-line comment (because bookmarklet is a one-liner, so you cannot close your comment)

3 - Some problems with semicolons ;.

Don't confuse statements (need semicolon) and assignments (do not need them).

Console has its "automatic semicolon insertion", but it is a problem for a one-lined bookmarklet.

You should read a bit more here: https://stackoverflow.com/a/2717956/5410914

Part II

Since there is no HTML-code to test, it might be hard to check. Moreover, you could have made it easier to read by making it multi-lined (it will still work as a bookmarklet when you paste it as a bookmark). But anyway.

The main reason it might not work is that you don't use IIFE-format (Immediately Invoked Function Expression).

Your code has function NoOverrideAction(t) and, moreover, there is };var noAction=new NoOverrideAction(window);noAction.run();)(); at the end. It is not IIFE.

Read more: https://developer.mozilla.org/ru/docs/Glossary/IIFE

To debug a bookmarklet on your own, since there is no HTML provided, you might try to make your code simplier (even with alert('hi'); at first, and verify the reason it won't start) and then make it more complicated once you figure out that it works.

ZolVas
  • 190
  • 2
  • 16