- Joined
- Nov 1, 2015
- Messages
- 46
- Reaction score
- 26
- First Language
- English
- Primarily Uses
=== EDIT ===
After better looking at the problem, I found an easier, less hacky crossbrowser solution, by using https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript
function getFileName(){ var srcParts = document.currentScript.src.split('/') return srcParts[srcParts.length-1].replace('.js', '')}var parameters = PluginManager.parameters(getFileName())=== Original post: don't use this, use solution above instead ===
If you want to get the filename of your plugin (for example to get your parameters independent of the file name)
you can throw and immediately catch an Error in your IIFE, and then use .stack to get the filename like this:
var path = require('path')try { throw new Error()} catch(e) { var fileName = e.stack.split('\n')[1].split(path.sep) fileName = /(.*)\.js:/.exec(fileName[fileName.length-1])[1]}var parameters = PluginManager.parameters(fileName)// same as: // var parameters = $plugins.filter(function(plugin){ return plugin.name === fileName })[0].parameters;if you are only interested in the parameter you can simplify it to:
After better looking at the problem, I found an easier, less hacky crossbrowser solution, by using https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript
function getFileName(){ var srcParts = document.currentScript.src.split('/') return srcParts[srcParts.length-1].replace('.js', '')}var parameters = PluginManager.parameters(getFileName())=== Original post: don't use this, use solution above instead ===
If you want to get the filename of your plugin (for example to get your parameters independent of the file name)
you can throw and immediately catch an Error in your IIFE, and then use .stack to get the filename like this:
var path = require('path')try { throw new Error()} catch(e) { var fileName = e.stack.split('\n')[1].split(path.sep) fileName = /(.*)\.js:/.exec(fileName[fileName.length-1])[1]}var parameters = PluginManager.parameters(fileName)// same as: // var parameters = $plugins.filter(function(plugin){ return plugin.name === fileName })[0].parameters;if you are only interested in the parameter you can simplify it to:
Code:
var path = require('path')try { throw new Error()} catch(e) { var parameters = e.stack.split('\n')[1].split(path.sep) parameters = PluginManager.parameters(/(.*)\.js:/.exec(parameters[parameters.length-1])[1])}
Last edited by a moderator: