Value conversion when saving
![]() ![]() |
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. |
![]() ![]() |
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? |
![]() ![]() |
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. |
![]() ![]() |
Use the onSave and onOpen features to simply convert them.
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.
|
![]() ![]() |
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. |
![]() ![]() |
Arthaey Angosii wrote: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. |
![]() ![]() |
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. |
![]() ![]() |
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. |
![]() ![]() |
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. |
![]() ![]() |
Oh I see. Version 1.1.9 has |
![]() ![]() |
Thanks, that works just how I wanted it to! |
![]() ![]() |
No problem, and sorry for the confusion, hope the example code helps you at least lol. |


