How to get the source of a javascript bookmark from itself when running?

Brad Parks

I have a bookmark that's a JavaScript bookmark. For example:

javascript:alert('hi');

I'd like to be able to get the source of the currently running JavaScript bookmark, from within the bookmark itself, so in pseudocode:

javascript:alert(currentlyExecutingScript.text);

Which would alert

javascript:alert(currentlyExecutingScript.text);

How can I do this? I prefer cross browser solutions, but am completely fine with just Chrome specific solutions!

Why am I interested in this? Because I'm writing a bookmarklet that refers to itself.

Toothbrush

As location refers to the current page's URL, and JavaScript bookmarks do not change location, this is not possible in any current browsers.

However, it is possible to do what you want in JavaScript:

javascript:void function f(){alert(f.toString())}()

That will alert the following:

function f(){alert(f.toString())}

The toString() method, when called on a function, returns a string representing the source code of the function (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString).

Credit

@SergeSeredenko suggested to use void.

When using an immediately-invoked function expression, void can be used to force the function keyword to be treated as an expression instead of a declaration.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void#Immediately_Invoked_Function_Expressions

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to save a bookmark in Firefox with POST data?

来自分类Dev

How to save a bookmark in Firefox with POST data?

来自分类Dev

How to get Image.Source as string

来自分类Dev

How to handle socketIO when app is not running

来自分类Dev

How to get the tty in which bash is running?

来自分类Dev

Skip pauses when running a batch script from Task Scheduler

来自分类Dev

error while running "source .vimrc"

来自分类Dev

Facebook PHP SDK how to get user email from Facebook JavaScript SDK 2014

来自分类Dev

How to get the final result of javascript?

来自分类Dev

How do I recompile and reload Java source code while `lein repl` is running?

来自分类Dev

How to make Bottle print stacktrace when running through apache modwsgi?

来自分类Dev

How to automatically switch focus to python console when running script in Pycharm?

来自分类Dev

JavaScript: XMLHttpRequest() from Firefox addon to get zip

来自分类Dev

How to get type from String?

来自分类Dev

How to prevent user from accessing complete source of angularjs application

来自分类Dev

Java exception: "Can't get a Writer while an OutputStream is already in use" when running xAgent

来自分类Dev

how to get json values inside an array in javascript

来自分类Dev

How to get all key in JSON object (javascript)

来自分类Dev

Spring Boot incorrectly loads test configuration when running from eclipse+gradle

来自分类Dev

how to compare a table with itself under a condition in mysql

来自分类Dev

How to get a value from keyvalue pair list?

来自分类Dev

How to get the country code from CultureInfo?

来自分类Dev

How to get extreme points from UIImage

来自分类Dev

How to get a filename from an ImageView (ANDROID)

来自分类Dev

How to get the value from ArrayList<Object> in android

来自分类Dev

How to get the selected date from jquery datepicker

来自分类Dev

How to get status code from NSURLSessionDataTask?

来自分类Dev

How to get Wikipedia page from Wikidata Id?

来自分类Dev

How to get a particular element from Queue?

Related 相关文章

  1. 1

    How to save a bookmark in Firefox with POST data?

  2. 2

    How to save a bookmark in Firefox with POST data?

  3. 3

    How to get Image.Source as string

  4. 4

    How to handle socketIO when app is not running

  5. 5

    How to get the tty in which bash is running?

  6. 6

    Skip pauses when running a batch script from Task Scheduler

  7. 7

    error while running "source .vimrc"

  8. 8

    Facebook PHP SDK how to get user email from Facebook JavaScript SDK 2014

  9. 9

    How to get the final result of javascript?

  10. 10

    How do I recompile and reload Java source code while `lein repl` is running?

  11. 11

    How to make Bottle print stacktrace when running through apache modwsgi?

  12. 12

    How to automatically switch focus to python console when running script in Pycharm?

  13. 13

    JavaScript: XMLHttpRequest() from Firefox addon to get zip

  14. 14

    How to get type from String?

  15. 15

    How to prevent user from accessing complete source of angularjs application

  16. 16

    Java exception: "Can't get a Writer while an OutputStream is already in use" when running xAgent

  17. 17

    how to get json values inside an array in javascript

  18. 18

    How to get all key in JSON object (javascript)

  19. 19

    Spring Boot incorrectly loads test configuration when running from eclipse+gradle

  20. 20

    how to compare a table with itself under a condition in mysql

  21. 21

    How to get a value from keyvalue pair list?

  22. 22

    How to get the country code from CultureInfo?

  23. 23

    How to get extreme points from UIImage

  24. 24

    How to get a filename from an ImageView (ANDROID)

  25. 25

    How to get the value from ArrayList<Object> in android

  26. 26

    How to get the selected date from jquery datepicker

  27. 27

    How to get status code from NSURLSessionDataTask?

  28. 28

    How to get Wikipedia page from Wikidata Id?

  29. 29

    How to get a particular element from Queue?

热门标签

归档