GM_config

By JoeSimmons Last update Feb 26, 2011 — Installed 1,031,811 times.

Value conversion when saving

in
Subscribe to Value conversion when saving 12 posts, 2 voices



Arthaey Angosii Scriptwright
FirefoxWindows

I want to prompt the user for a time string, but store it internally as a numeric value.

I tried setting the value in the save() function, but it didn't actually save my converted value to the GM_config object. I also tried having two separate value, one type='text' and the other type='hidden', but that didn't work at all.

Is there any way to do what I'm trying to do? I would rather avoid running the conversion code every single time I want to use the value stored in GM_config.

 
JoeSimmons Script's Author
FirefoxWindows

What would be an example of a time string someone would enter? Need more info, like what do you mean you tried setting the value in the save function?

 
Arthaey Angosii Scriptwright
FirefoxWindows

For example, the user enters "1pm". I don't need to save the actual string value except to pre-populate the GM_config dialog window when it's reopened.

When the GM_config dialog is closed, I want to run a conversion function to turn the string "1pm" into the integer 13 (24-hour time). This integer is the value I use in all of my script's functions; as I said, I don't really care about the string version that the user entered.

 
JoeSimmons Script's Author
FirefoxWindows

Use the onSave and onOpen features to simply convert them.
Here's some example code since I had to modify the base code a little.

			save: function(e) {
			var time = GM_config.get("time").toString(),
				twentyfour = parseInt(time)+(time.indexOf("pm")!=-1?12:0);
			GM_config.set("time", twentyfour);
			},
			open: function(e) {
			var timeE = GM_config.frame.contentDocument.getElementById("field_time"),
				time = parseInt(timeE.value),
				twelvehour = (time>12?time-12:time)+(time>12?"pm":"am");
			timeE.value = twelvehour;
			}

Since I had to modify the base code, please update the script to 1.1.8.

 
Arthaey Angosii Scriptwright
FirefoxWindows

Your sample code doesn't quite do what I want.

I want the user to only see the "1pm" version in the dialog window. But I want to save the 24-hour integer for use by the code. So I don't want to update #field_time.value with the converted-to-integer value; I want it to stay as the user entered it.

 
JoeSimmons Script's Author
FirefoxWindows
Arthaey Angosii wrote:
Your sample code doesn't quite do what I want.
That's what it does for me. It shows, for example, 1pm in the box, and after you save it, it's turned to 13, and when the config is opened it's turned back to 1pm. You can see it's actually stored as a 24-hour time if you use GM_config.get("timename") after the config box is saved & closed; actually you can stick an alert straight in the onClose function.
If you're not seeing, that, please update GM_config to 1.1.8.

Also, it was just example code, you're supposed to modify it to do what you need since onOpen and onSave aren't built in functions.
 
Arthaey Angosii Scriptwright
FirefoxWindows

There is still a slight difference. If the user initially typed in "1:00 PM" or "1pm" or "1 PM" or just "13", I want to display it in that format. Your suggestion will have the string normalized to only one format, because it's calculated.

 
JoeSimmons Script's Author
FirefoxWindows

I'm sorry but that's really not my problem. You'll have to figure out which format it is and convert it to your liking.

 
Arthaey Angosii Scriptwright
FirefoxWindows

Yes, I understand that this requirement is my requirement.

My initial question was asking about ways of having hidden config values, so I could do my own conversion code without the user seeing one of the values in the GM_config object.

I suppose I could fork the existing code and add a way to "hide" config values myself, if this isn't something you see yourself supporting officially.

 
JoeSimmons Script's Author
FirefoxWindows

Oh I see. Version 1.1.9 has type:'hidden' included, plus some visual changes to the sections and section headings.

 
Arthaey Angosii Scriptwright
FirefoxX11

Thanks, that works just how I wanted it to!

 
JoeSimmons Script's Author
FirefoxWindows

No problem, and sorry for the confusion, hope the example code helps you at least lol.

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel