MozillaZine

Load page AND run bookmarklet js with ONE bookmark?

User Help for Mozilla Firefox
semigeek
 
Posts: 98
Joined: April 16th, 2014, 8:54 pm
Location: Colorado

Post Posted January 18th, 2019, 11:54 am

How can I set up a bookmark(let) to load a page and then run some bookmarklet js on that page, all with one click? I have the code I need working (with some trial and error, not an expert) but don't know how to combine the two. Thanks.

morat
 
Posts: 3445
Joined: February 3rd, 2009, 6:29 pm

Post Posted January 18th, 2019, 12:47 pm

You can't load another page, then run a code snippet with a bookmarklet.

e.g.

Code: Select all
javascript:(function () {
  location.replace("http://www.mozillazine.org/");
  setTimeout(function () {
    alert("This alert will not display.");
  }, 5000);
})();

Bookmarklet Builder
http://subsimple.com/bookmarklets/jsbuilder.htm

semigeek
 
Posts: 98
Joined: April 16th, 2014, 8:54 pm
Location: Colorado

Post Posted January 19th, 2019, 11:00 am

I wondered whether that was possible... it's OK, what I have is working well as a two-click solution. Thanks for the link also, I look forward to exploring it.

morat
 
Posts: 3445
Joined: February 3rd, 2009, 6:29 pm

Post Posted January 19th, 2019, 5:52 pm

You could use a userscript to execute a code snippet after opening a bookmark with a query string.

Bookmark 1 opens normally. Bookmark 2 executes a code snippet after opening.

Bookmark 1
http://www.mozillazine.org/

Bookmark 2
http://www.mozillazine.org/index.html?example=alpha

Code: Select all
// ==UserScript==
// @name         Test
// @match        http://www.mozillazine.org/*
// @grant        none
// ==/UserScript==

(function () {

  "use strict";

  function getQuery(name) {
    var params = location.search.substring(1).split("&");
    for (var i = 0; i < params.length; i++) {
      var pair = params[i].split("=");
      if (pair[0] == name) {
        return pair[1];
      }
    }
    return null;
  }
  if (getQuery("example") == "alpha") {
    // query found, execute code snippet
  } else {
    // query not found, do nothing
  }

})();

Return to Firefox Support


Who is online

Users browsing this forum: No registered users and 6 guests