Chrome Bookmarklet Works When Clicked, But Not When "Launched": Why?

Chrome Bookmarklet Works When Clicked, But Not When "Launched": Why?

I have a bookmarklet that fills in a form for me, and it works great when I click on it in my bookmarks toolbar.

However, when I hit CTRL + L (the "launch" shortcut, which takes you to the URL bar) and type in the bookmarklet's name (and hit ENTER), it fails to work. In the console there's an error about "Unable to find element" (presumably the form element it's trying to fill in).

Can anyone explain why the bookmarklet would work, and be able to access the form, in my bookmarks toolbar, but not when I "launch" it from the URL bar?

答案1

  1. A bookmarklet (JavaScript code) runs on the existing page clicked (so the code to fill the form runs on the open tab of the form), so you can't open the bookmarklet by itself
  2. Control+L is not Launch, it's Jump to the address bar and don't do anything, so it's just like clicking into the address bar, not sure why you think that should do something
  3. You enter the bookmarklet name into the address bar? If something you need to paste the whole code into the address bar (and after pasting you need to go to the beginning of it and add javascript: before the code)

If you want to be able to easily run the bookmarklet with a keyboard shortcut, you'll need something more.

You can set up something like this with a Chrome Extension like Shortkeys (Custom Keyboard Shortcuts), it allows you to set up a shortcut to trigger a bookmark that you have (see some info on it in this guide https://www.groovypost.com/howto/add-shortcut-keys-chrome-bookmarks/, something like this: enter image description here

答案2

After @Yisroel Tech suggested I take a screenshot I went back to the web page and ... CTRL + L (seemingly magically) now works.

I'm not sure what fixed it, but I did restart my computer (which meant also restarting my browser), so if anyone else has trouble running a bookmarklet with CTRL + L I'd recommend trying that first.

相关内容