{"id":721,"date":"2021-11-23T18:52:51","date_gmt":"2021-11-23T17:52:51","guid":{"rendered":"http:\/\/dominic.no\/blog\/?p=721"},"modified":"2021-11-23T18:52:51","modified_gmt":"2021-11-23T17:52:51","slug":"save-and-load-more-reliably-with-in-userscript-maybe","status":"publish","type":"post","link":"https:\/\/dominic.no\/blog\/save-and-load-more-reliably-with-in-userscript-maybe\/","title":{"rendered":"Save and load more reliably with in userscript, maybe."},"content":{"rendered":"\n<p>So, been messing around with userscript lately, in tampermonkey. And hence had a few problems saving and loading with GM_setValue and GM_getValue. There are a couple of scrips out there to sort this for you, but none that i was able to get to work. So I&#8217;ve wrapped my own version up, which also compresses via LZstring. (google it).<\/p>\n\n\n\n<p>d.save(&#8220;float&#8221;, 12.34);<br>d.load(&#8220;float&#8221;);<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>For my purpose i found it most reliable to include whole libraries inside an anonymous function together with any code. This seem to help som scope issues I had, at a potential cost of performance (none that I have noticed tho). I&#8217;ve also included waitForKeyElement, cause it is nice to have at hand.<\/p>\n\n\n\n<p>In principle the functions just wrap anything into an object (which you dont really need to do, but wth), stringifies, saves MD5, compresses, saves. And vice versa.<\/p>\n\n\n\n<p>It does not work as expected with any data. Seems safe for sring, int, float, bool,  undefined, null, objects and arrays. Does not work for NaN or functions as far as i can tell. Also not sure about &#8220;new Date()&#8221; objects and the like.<\/p>\n\n\n\n<p>However for all intents and purposes it seem reliable enough.<\/p>\n\n\n\n<p>The whole ting is included in a file further down, with some examples. Here are the main methods themselves.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">var<\/span> d = {\n    <span class=\"hljs-attr\">save<\/span> : <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\">key, value<\/span>)<\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (key){\n            <span class=\"hljs-keyword\">var<\/span> wrobj = {<span class=\"hljs-attr\">value<\/span>: value};\n            <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-built_in\">Number<\/span>.isNaN(value)){wrobj = {<span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">\"_NaN_\"<\/span>}};\n            <span class=\"hljs-keyword\">try<\/span> {\n                <span class=\"hljs-keyword\">var<\/span> strng = <span class=\"hljs-built_in\">JSON<\/span>.stringify(wrobj);\n            } <span class=\"hljs-keyword\">catch<\/span> (e){\n                <span class=\"hljs-built_in\">console<\/span>.log(e);\n                <span class=\"hljs-keyword\">return<\/span>;\n            }\n            <span class=\"hljs-keyword\">var<\/span> hash = MD5(strng);\n            strng = LZString.compressToBase64(strng);\n            GM_setValue(key, strng);\n            GM_setValue((key + <span class=\"hljs-string\">\"hash\"<\/span>), hash);\n        }\n    },\n    <span class=\"hljs-attr\">load<\/span> : <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\">key<\/span>)<\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (key){\n            <span class=\"hljs-keyword\">if<\/span> (GM_getValue(key)){\n                <span class=\"hljs-keyword\">var<\/span> strng = GM_getValue(key);\n                strng = LZString.decompressFromBase64(strng);\n                <span class=\"hljs-keyword\">var<\/span> storedhash = GM_getValue(key + <span class=\"hljs-string\">\"hash\"<\/span>);\n                <span class=\"hljs-keyword\">var<\/span> newhash = MD5(strng);\n                <span class=\"hljs-keyword\">if<\/span> (storedhash === newhash){\n                    <span class=\"hljs-keyword\">try<\/span> {\n                        <span class=\"hljs-keyword\">var<\/span> value = <span class=\"hljs-built_in\">JSON<\/span>.parse(strng);\n                    } <span class=\"hljs-keyword\">catch<\/span> (e){\n                        <span class=\"hljs-built_in\">console<\/span>.log(e);\n                        <span class=\"hljs-keyword\">return<\/span>;\n                    }\n                    <span class=\"hljs-keyword\">return<\/span> value.value;\n                }\n            } <span class=\"hljs-keyword\">else<\/span> {\n                <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">false<\/span>;\n            }\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/dominic.no\/blog\/wp-content\/uploads\/2021\/11\/save-load-any.user_.js\">save-load-any.user<\/a><a href=\"http:\/\/dominic.no\/blog\/wp-content\/uploads\/2021\/11\/save-load-any.user_.js\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>So, been messing around with userscript lately, in tampermonkey. And hence had a few problems saving and loading with GM_setValue and GM_getValue. There are a couple of scrips out there to sort this for you, but none that i was able to get to work. So I&#8217;ve wrapped my own version up, which also compresses via LZstring. (google it). d.save(&#8220;float&#8221;, 12.34);d.load(&#8220;float&#8221;);<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"spay_email":""},"categories":[1],"tags":[],"gutentor_comment":0,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":1,"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":723,"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions\/723"}],"wp:attachment":[{"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dominic.no\/blog\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}