-
Posts
1,033 -
Joined
-
Last visited
-
Days Won
17
Everything posted by gw1500se
-
I'm a bit confused about regexp in Javascript. If I have a string that is in the form of a regexp do I need to create a regexp object to use it? If so do I just do: expr=new RegExp(str); TIA.
-
You need to post your code. What did you try? What error did you get or what did you get that was not expected?
-
This appears to be related to special restrictions imposed by Chrome extensions. I've moved to stackoverflow for help.
-
Are you trying to get 3 levels deep? myArr[0][0][0]
-
That is the entire script and no, I am not getting an error. The big difference is that my code is running as a Chrome extension.
-
Except that didn't work either originally.
-
If your conditional fails then that variable is not set. You only set it in the else clause. You need to initialize it to something or test if it exists before using it.
-
Now that I have another issue fixed, I am back to the problem of no cookie being written. This is my current code: function create_cookie(obj=null) { var json_array=[]; if (obj==null) { json_array=[{none:true}]; } else { json_array=obj; } Cookies.set("Auto_Select_0519669",JSON.stringify(json_array),{expires:3650}); } var json_str=Cookies.get("Auto_Select_051969"); if (typeof(json_str)=="undefined") { create_cookie(); json_str=[{none:true}]; console.log("set new cookie"); } console.log(json_str); Each time I run this code I get the output "set new cookie". When I search for the cookie it it not found. Apparently 'Cookies.set' is not writing the cookie but I get no errors.
-
I finally got it working but I can't explain how/why. I did: console.log(typeof(json_str)=="undefined"); The output was "true" which I expected. I then edited the line by changing 'console.log' to 'if' and changed the ';' to '{' and the thing started to work.
-
Yes. With or without the parens, I get the same result.
-
I an trying to determine if a cookie exists using the following code: const json_str=Cookies.get("Auto_Select_051969"); if (!(json_str in window)) { create_cookie(); json_str=Cookies.get("Auto_Select_051969"); } json=JSON.parse(json_str); When it gets to the parse I get the error: Uncaught SyntaxError: "undefined" is not valid JSON at JSON.parse (<anonymous>) at cookie_mgmt.js:16:11 'create_cookie()' is not executed. If 'json_str' is undefined, per the error, how can it fail the 'if' test? I also tried: if (typeof(json_str)=="undefined") { Which also fails the 'if' test.
-
Deleted for now.
-
Using that library, at least the way I read it, does not work. This is my function which produces "undefined" in the console log (no errors): function create_cookie(obj=null) { var json_array=[]; if (obj==null) { json_array=[{filter:false}]; } else { json_array=obj; } Cookies.set("Auto_Select_0519669",JSON.stringify(json_array),{expires:2147483647}); } console.log(Cookies.get("Auto_Select_0519669"));
-
I have to let my javascript ignorance show here. It seems that package required 'imports'. Since I am writing a Chrome extension, imports are not permitted. I need the js file(s) themselves and I don't see that in the git tree.
-
I don't know if I have a mental block or if the documentation is just poor. In any case I am trying to create a cookie with this code: const daysToExpire = new Date(2147483647 * 1000).toUTCString(); document.cookie=escape("Auto_Select_0519669="+JSON.stringify(json_array)+";expires="+daysToExpire+";"); Nothing is written. What am I dong wrong or not doing? TIA.
-
Yes, really. Those never showed up in any of my searches. Thanks for the links.
-
I can't find this documented any where but what responses are available other than json? I have this function: function requests(url) { return fetch(url).catch(function(error) { return(null); }) .then(function(response) { if (response==null) { return(null); } else if (!response.ok) { console.log("caught error: "+response.status); } try { return response.json(); } catch(e) { return resonse.html(); } }); } The response can be either json or html. When html results I get an error: VM13:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON How do I tell the difference since try/catch doesn't work? TIA.
-
Close but I'm still getting an error in the console. Maybe it doesn't matter but I can't seem to eliminate or catch this error: /projects/3U8PAUGKO25K6476BGLE7U090QYNCN/tasks/accept_random.json?ref=w_pl_prvw projects/3U8PAUGKO25K6476BGLE7U090QYNCN/tasks/accept_random.json?ref=w_pl_prvw:1 Failed to load resource: net::ERR_FILE_NOT_FOUND Is that simply not possible and there is not way to suppress it. Otherwise everything seems to be working.
-
I'm getting the error and catching it. I don't want the error thrown. Here is my code: function requests(url) { return fetch(url).catch(function(error) { console.log("caught error: "+error); }) .then(function(response) { if (!response.ok) { throw new Error(`Request failed with status ${response.status}`); } return response.json(); }); } async function myExtension() { const data = await requests("https://worker.mturk.com/projects.json"); for (let item of data.results) { console.log(item.accept_project_task_url); const data = await requests(item.accept_project_task_url); console.log(data); break; } } myExtension(); This is the console result: /projects/3YATNUOYWPGCVB7NH3IDJB44X40R53/tasks/accept_random.json?ref=w_pl_prvw projects/3YATNUOYWPGCVB7NH3IDJB44X40R53/tasks/accept_random.json?ref=w_pl_prvw:1 Failed to load resource: net::ERR_FILE_NOT_FOUND auto_select.js:3 caught error: TypeError: Failed to fetch auto_select.js:6 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'ok') at auto_select.js:6:21 at async myExtension (auto_select.js:17:20) Only the bold line is what I expected to show up so I can handle it. Is there a coding error that is causing the rest?
-
Yes, this is an external web site. I'll give that a try. They sure don't make this intuitively obvious.
-
The problem is that response.ok is true even with that error. I don't understand why unless the error is being thrown by 'fetch(url)' rather than 'function(response)'.
-
I have the following script that does a fetch: function requests(url) { return fetch(url).then(function(response) { if (!response.ok) { throw new Error(`Request failed with status ${response.status}`); } return response.json(); }); } There is one error that is occasionally expected: Failed to load resource: net::ERR_FILE_NOT_FOUND I need to handle that error and return something that indicates it to an async calling function. If I understand the code the error is coming from fetch(url) not function(response). I think I need to add a .catch somewhere to handle that error, right? As an aside, will function(response) ever really return a false for response.ok?