Execute Javascript in URL (plugins ..)

1

How do I run JavaScript snippets in the address bar, I tried in Firefox something like: javascript: alert("lol") , but it seems to be an old technique that does not work (at least not in Firefox).

The idea is to have a bookmark in the bookmarks bar in which it is a snippet of JavaScript so that I can click on it and it does some procedures on the current page (in which the user is present), such as displaying the address of the page, capturing an element value in the DOM.

Is it possible? There are special plugins for this (firefox, chrome ..), and would it be possible to use JQuery to make coding easier?

    
asked by anonymous 17.11.2015 / 15:54

3 answers

3

The name of this feature is bookmarklet and does not depend on plugins to work. According to Wikipedia:

  

A bookmarklet is a small JavaScript program that is stored as a URL in Favorites.

bookmarklets are favorites that use the javascript: protocol instead of http:// or https://

The most popular method to create bookmarklets is to create a link with the <a> tag that can be "installed" by clicking and dragging the link to the bookmarks bar.

<a href="javascript:alert('Olá mundo');">Olá mundo</a>

You can use jQuery in bookmarklet , but you must include the library manually (code adapted from Bookmarklet Creator with Script Includer ):

javascript:(function(){
  // cria script
  var s = document.createElement("script");
  s.src = 'http://code.jquery.com/jquery.js';
  // adiciona para executar depois que carregar
  s.addEventListener('load', function() {
    (function($){
      var jQuery = $;
      // seu código entra aqui
      // jQuer já estará carregado
    })(jQuery.noConflict(true))
  }, false);
  // adiciona script na página
  document.body.appendChild(s);
})()

In this implementation, you just need to check if jQuery is already loaded on the page.

    
17.11.2015 / 17:44
4

You can save as favorite as you wish.

  

I'm running firefox version 42.0 and it works like this:

Add the page as a bookmark.

  

Ctrl + D and finish.

Afterthat,gotothepagethatyouadded,rightclickonitandclickproperties.

Onceyou'vedonethis,pasteyourjavascriptinplaceoftheurlandclicksave.Yourjavascriptshouldfollowthis"syntax":

  

javascript: alert ('Lol');

Donethis,justclickonthebookmarkasitwillrunthescript.

Another way is to go directly to your browser, delete the link you have, type script and press enter.

Finally, you can do via the browser console (Ctrl + Shift + J) and enter the script you want.

  

These forms were tested on Firefox 42, Google Chrome 42, and Internet Explorer 11.

    
17.11.2015 / 16:20
0

In addition to the excellent response you have chosen, the Sanction , a solution of if you put the execution of a bookMarkLet in the extensions bar of the ChroMe family (ChroMe, Canary, ChroMium):

Bare minimum Chrome extension to inject to JS file into the given page when you click on the browser action icon. The script then inserts a new div into the DOM.

NOTE: Put the desired script / bookMarkLet into the [inject.js] file, right after the code snippet:

  

document.body.appendChild (div);

In order to insert the extension mentioned above into ChroMe, proceed as follows:

  • Open the ChroMe extensions address: (chrome: // extensions /)
  • Enable [Developer Mode], located at the top right of the screen
  • Click "Load without compression"
  • Select the folder of the extension you downloaded above (the folder containing the manifest.json file)
  • Confirm, and you're done!
  • Just click on the new extension icon and then the script or call to the desired bookMarkLet will be executed
  • If you want a shortcut to this new extension open the menu option [Extensions - > Keyboard Shortcuts - > New extension] and add the shortcut

REFERENCE: Stack OverFlow: How to modify an extension from the Chrome Web Store?

    
11.02.2018 / 18:19