brookhong / Surfingkeys

Map your keys for web surfing, expand your browser with javascript and keyboard.
https://chrome.google.com/webstore/detail/surfingkeys/gfbliohnnapiefjpjlpjnehglfpaknnc
MIT License
5.21k stars 472 forks source link

[FR] Running JavaScript bookmarklet via hotkeys #1866

Open Archie-2021 opened 1 year ago

Archie-2021 commented 1 year ago

I have JS bookmarklets and I like to trigger them via hotkey, Is there a way to do that? Opening them like a bookmark don't work. Can I add them to settings as a function and run them from there? If the answer is yes how? Sorry but I am new to coding and don't know a lot of stuff still trying to learn.

And thank you for this awesome extension, it makes a lot of difference in my workflow.

Madd0g commented 1 year ago

It is technically possible to run arbitrary JS on the page with insertJS

Opening them like a bookmark don't work.

What have you tried? I tried before to run JS code from a custom omnibar, I even had that attempt still commented out in my code:

  var myUrls = [
      {
        title: "Home Page",
        url: "javascript:alert('home')",
      },
      {
        title: "wp admin",
        url: "javascript:alert('admin')",
      },
    ];
    Front.openOmnibar({ type: "UserURLs", extra: myUrls });

So I'm guessing that at least from the omnibar, it should be possible to trigger JS that starts with javascript:

Archie-2021 commented 1 year ago

It is technically possible to run arbitrary JS on the page with insertJS

Opening them like a bookmark don't work.

What have you tried? I tried before to run JS code from a custom omnibar, I even had that attempt still commented out in my code:

  var myUrls = [
      {
        title: "Home Page",
        url: "javascript:alert('home')",
      },
      {
        title: "wp admin",
        url: "javascript:alert('admin')",
      },
    ];
    Front.openOmnibar({ type: "UserURLs", extra: myUrls });

So I'm guessing that at least from the omnibar, it should be possible to trigger JS that starts with javascript:

Interesting, I just tried to open JS bookmarklets from bookmarks (b mapping) omnibar. can you explain more how to trigger this custom omnibar? I like to give it a try. I should put this in settings put my JS code as url and then what should I map it to?

Madd0g commented 1 year ago
mapkey("co", "Custom omnibar", function () {
     var myUrls = [
      {
        title: "Home Page",
        url: "javascript:alert('home')",
      },
      {
        title: "wp admin",
        url: "javascript:alert('admin')",
      },
    ];
    Front.openOmnibar({ type: "UserURLs", extra: myUrls });
});
Archie-2021 commented 1 year ago
mapkey("co", "Custom omnibar", function () {
     var myUrls = [
      {
        title: "Home Page",
        url: "javascript:alert('home')",
      },
      {
        title: "wp admin",
        url: "javascript:alert('admin')",
      },
    ];
    Front.openOmnibar({ type: "UserURLs", extra: myUrls });
});

I copied this to my setting, first it was giving me "mapkey undefined" error. I added api. before key map and know it shows custom omnibar option in the c menu but when I hit co nothing happens

tmrony commented 11 months ago
api.mapkey("co", "Custom omnibar", function () {
     var myUrls = [
      {
        title: "Home Page",
        url: "javascript:alert('home')",
      },
      {
        title: "wp admin",
        url: "javascript:alert('admin')",
      },
    ];
    api.Front.openOmnibar({ type: "UserURLs", extra: myUrls });
});

Adding api. before those 2 lines work for me.

brookhong commented 3 weeks ago

This is currently supported, but it is planned to be removed in migration to MV3 due to a more strict restriction on execution of arbitrary JS code.