this post was submitted on 24 Aug 2023
40 points (100.0% liked)

Firefox

18040 readers
231 users here now

A place to discuss the news and latest developments on the open-source browser Firefox

founded 5 years ago
MODERATORS
 

In Firefox you can combine JS bookmarklets, keywords and params to do something like this:

javascript:(function(){
    var args = '%s'.split(' ');
    alert(args);
})();

Useful example:

javascript:(function(){
    var args = '%s'.split(' ');
    var subreddit = args[0];
    var search = args[1];
    document.location.href = "https://www.reddit.com/r/" + subreddit + "/search/?q=" + search + "&include_over_18=on&restrict_sr=on&t=all&sort=new";
})();

Bookmarklet format:

javascript:(function() {var args = '%s'.split(' ');var subreddit = args[0];var search = args[1];document.location.href = "https://www.reddit.com/r/" + subreddit + "/search/?q=" + search + "&include_over_18=on&restrict_sr=on&t=all&sort=new";})();

If you assign the keyword redditsearch to that bookmarklet, you can type redditsearch PixelArt zelda on the firefox navbar and you will be reditected to the Reddit search for 'zelda' on r/PixelArt.

In general this makes the navbar a very powerful command line in which you can add any command with multiple params.


It seems Mozilla has plans to get rid of this feature, see the ticket Migrate keyword bookmarks into about:preferences managed Search Engines. The good news is that the last comment, besides mine asking them not to remove this functionality, is from some years ago. I hope they change their mind, or forget about it...


TIP: If you don't want to remember the param order, you can also ask for them with a prompt if no arguments are specified:

javascript:(function(){
    var args = '%s'.split(' ');
    var subreddit = args[0] != "" ? args[0] : prompt("Enter the subbreddit:");
    if (!subreddit) return;
    var search = args.length > 1 ? args[1] : prompt("Enter the text to search:");
    if (!search) return;
    document.location.href = "https://www.reddit.com/r/" + subreddit + "/search/?q=" + search + "&include_over_18=on&restrict_sr=on&t=all&sort=new";
})();

Bookmarklet format:

javascript:(function(){ var args = '%s'.split(' '); var subreddit = args[0] != "" ? args[0] : prompt("Enter the subbreddit:"); if (!subreddit) return; var search = args.length > 1 ? args[1] : prompt("Enter the text to search:"); if (!search) return; document.location.href = "https://www.reddit.com/r/" + subreddit + "/search/?q=" + search + "&include_over_18=on&restrict_sr=on&t=all&sort=new"; })();

Sorry for the reddit examples, this was posted originally on r/firefox some time ago and adapting them to lemmy seemed a bit too much work :P.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 1 year ago (1 children)

Accessing the places database directly would be great of course

The problem is not accessing the DB (I manage to make that work, see the previous link). I was even able to execute the UPDATE... but there is a hash to prevent unwanted modifications. I found some python code that should calculate the hash, but Firefox did not like it... or I missed some detail (encoding maybe?). Personally, the workflow I achieved with that script is good-enough for me, but it's far from ideal.

The other usual approach to scripting in the browser is ViolentMonkey (or TamperMonkey, or others), much better for big scripts. The downside is that (AFAIK) you need to inject in every page all the scripts you want to be able to execute and (again, AFAIK) there is no way to run them as easy as "Control+L" (focus navbar) > Type keyword... which I have now so ingrained in my brain :D.

[–] deergon 2 points 1 year ago (1 children)

I tried the python code out and it actually does work for me, ie. I'm able to update an existing bookmark.

# hashing code from link inserted here
def update_place(id, new_url):
    new_url_hash = url_hash(new_url)
    conn = sqlite3.connect('places.sqlite')
    cur = conn.cursor()
    cur.execute('UPDATE moz_places SET url = ?, url_hash = ? WHERE id = ?', (new_url, new_url_hash, id))
    conn.commit()
    cur.close()
    conn.close()

update_place(16299, "javascript:alert('Testing bookmarklet update ...');alert('Great success!');")

Only annoying thing is that Firefox needs to be closed while updating. Anyway, I haven't tried with bigger scripts though, so there might be some gotchas there.

Agreed, keywords are really nice for keyboard navigation!

[–] [email protected] 2 points 1 year ago

Thanks for the testing! I'm happy it works :D.

I think I only tried to execute the UPDTE directly from the batch file instead of doing it from python. Good idea!

Only annoying thing is that Firefox needs to be closed while updating.

I don't remember if I tried that. It kinda makes sense... I don't think it's expected to hot edit those links :P.