Load page AND run bookmarklet js with ONE bookmark?

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

Load page AND run bookmarklet js with ONE bookmark?

Post by semigeek »

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: 6404
Joined: February 3rd, 2009, 6:29 pm

Re: Load page AND run bookmarklet js with ONE bookmark?

Post by morat »

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: 322
Joined: April 16th, 2014, 8:54 pm
Location: Colorado

Re: Load page AND run bookmarklet js with ONE bookmark?

Post by semigeek »

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: 6404
Joined: February 3rd, 2009, 6:29 pm

Re: Load page AND run bookmarklet js with ONE bookmark?

Post by morat »

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
  }

})();
Post Reply