kentaromiura

Veteran
Veteran
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:

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:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,877
First Language
English
This is pretty nice.


I had saw the stacktrace before but didn't think of using it that way.


Does this work for all platforms/browsers?


MDN says the stack property is non-standard.
 
Last edited by a moderator:

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,602
Reaction score
3,832
First Language
Java's Crypt
Primarily Uses
RMMZ
While this is a clever trick, I think our current solution is better and more reliable for getting the parameters.


One thing I noticed is that you used require, which is only available on windows and mac. But since you only used it to get the path separator, that can be ignored.


Another thing is that when I ran that code, my filename was in another position of the array.
 

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
While this is a clever trick, I think our current solution is better and more reliable for getting the parameters.One thing I noticed is that you used require, which is only available on windows and mac. But since you only used it to get the path separator, that can be ignored.Another thing is that when I ran that code, my filename was in another position of the array.
Ah, right didn't try to export as mobile, I may check what happens there
 

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
@hudell, @tsukihime, I updated my solution so now it's crossbrowser, works in nw.js as well, did not try exporting the project as mobile yet, if plugins are minified it may break, but it's addressable in the build task with a simple transform.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,602
Reaction score
3,832
First Language
Java's Crypt
Primarily Uses
RMMZ
Ohh, I liked that solution!

You put a comma where it should be a semi-colon, but other than that, it's perfect and it should work on everything other than Internet Explorer and Opera Mobile.
 

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
Ohh, I liked that solution!

You put a comma where it should be a semi-colon, but other than that, it's perfect and it should work on everything other than Internet Explorer and Opera Mobile.
In IE the gl context needed by pixi is missing as well, should work on edge, the comma is my fault, I merged the last two line in one but I forgot to remove the comma, fixed now
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
868
First Language
German
Primarily Uses
This should be a working one-line version:

PluginManager.parameters(/\/(?!.+\/)(.+?)\.js/.exec(document.currentScript.src)[1]);It captures everything between a slash not followed by another slash (so, the last part of the file path) and ".js". Since you have to be inside the script to execute this code in the first place, the regex is guaranteed to match.
 

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
This should be a working one-line version:

PluginManager.parameters(/\/(?!.+\/)(.+?)\.js/.exec(document.currentScript.src)[1]);It captures everything between a slash not followed by another slash (so, the last part of the file path) and ".js". Since you have to be inside the script to execute this code in the first place, the regex is guaranteed to match.
if nested folder are allowed inside the plugin folder you need to add a end of string token ($) as anything.js is a valid folder
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
868
First Language
German
Primarily Uses
/edit: Sorry, i misread your post. You're right and i should modify it like this:

Code:
PluginManager.parameters(/\/(?!.+\/)(.+?)\.js$/.exec(document.currentScript.src)[1]);
That said, i never tried to add subfolders to the "plugins" folder, so if they would be part of the plugin name, you'd need to modify the regex to capture everything between "/js/plugins/" and ".js".
 
Last edited by a moderator:

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
Not even sure it will work, so it may not be an issue; btw typing on a train to work is tricky
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
868
First Language
German
Primarily Uses
On second thought, the "$" isn't even necessary. Since i'm matching from the last slash, a path like this:


"/path/to/plugins.js/pluginName.js"


Will still get matched to "pluginName".
 

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
I remember now why I splitted, it's because that was part of the stacktrace solution which in nw.js returns the file: protocol full path, to avoid making the regex too verbose I figure out I could just split by the path separator

in the currentScript solution you should get what src is in the markup so script/*, also you're right your regex doesn't need the end of string for the reason you stated
 

Latest Threads

Latest Profile Posts

My ex put my heart upon a self he said don't give me no lines and keep my hands to myself :,(
An Old... old Harold | RPG Maker News #71

Did a test on myself and I was negative on COVID. I was sitting near my aunt the whole time while doing stuff.
RPG Maker Games Critique with Studio Blue starts now! Join us and follow along live as we explore A Thief's Voyage by walldeaf.

Forum statistics

Threads
112,242
Messages
1,066,798
Members
145,853
Latest member
dantecostello
Top