Jump to content

gw1500se

Members
  • Posts

    1,029
  • Joined

  • Last visited

  • Days Won

    16

Everything posted by gw1500se

  1. You need to post your code. What did you try? What error did you get or what did you get that was not expected?
  2. This appears to be related to special restrictions imposed by Chrome extensions. I've moved to stackoverflow for help.
  3. Are you trying to get 3 levels deep? myArr[0][0][0]
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. Yes. With or without the parens, I get the same result.
  9. 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.
  10. 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"));
  11. 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.
  12. 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.
  13. Yes, really. Those never showed up in any of my searches. Thanks for the links.
  14. 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.
  15. 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.
  16. 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?
  17. Yes, this is an external web site. I'll give that a try. They sure don't make this intuitively obvious.
  18. 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)'.
  19. 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?
  20. Thanks. I don't need the '[1]' but I have what I want now. On to the next layer of the onion.
  21. First this is a Chrome extension. I haven't developed any more code yet since I am not sure what elements will be returned. The results of that fetch produces a large array of json elements. Depending on filters I have to build to analyze each array element, I need to issue another fetch for specific elements. After processing that, if it meets certain criteria, I will pass that information to another extension's API. The process will need to continue to be repeated every 'n' time periods. Perhaps I need an alternative method for getting my data but fetch seems like the only way to get data from a URL in javascript.
  22. OK, although that seems impractical given what I need to do with the array contents. I think the function then becomes superfluous if I can't get anything outside of it. We no longer have a .then. I don't know where that would go given the current code.
  23. There isn't any more code (I guess that is the problem). Are you saying I need another .then to return the results of the fetch?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.