background.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. console.log(`background: ${new Date().getTime()}`)
  2. async function sleep( sec=1){
  3. milisec = sec * 1000;
  4. return new Promise ( resolve => {
  5. setTimeout(resolve,milisec);
  6. console.log(`slept for ${sec} seconds` );
  7. }
  8. );
  9. }
  10. function dataInitial() {
  11. const dataSync = browser.storage.local.get();
  12. dataSync.then(
  13. function onGet(item) {
  14. if (Object.keys(item).length === 0) {
  15. console.info('no data set let set some default')
  16. let tmpdata = {
  17. "auth": {
  18. "user": "admin",
  19. "password": "password",
  20. "client_id": "EvaluationClientId",
  21. "client_secret": "password",
  22. "password_type": "query"
  23. },
  24. "setting": {
  25. "ison": true,
  26. },
  27. };
  28. browser.storage.local.set(tmpdata);
  29. return tmpdata;
  30. }
  31. else {
  32. // console.info("some data was set")
  33. for (element in item) {
  34. // console.table(element);
  35. // console.table(item[element]);
  36. };
  37. return item;
  38. }
  39. },
  40. function onError(error) {
  41. console.table(error);
  42. return null;
  43. }
  44. );
  45. }
  46. window.swaggersettings = 1;
  47. browser.runtime.onInstalled.addListener(dataInitial)
  48. browser.runtime.onMessage.addListener(response);
  49. async function response(message, sender,resp) {
  50. // response("ok");
  51. switch (message.iNeed) {
  52. case "setting":
  53. await null;
  54. resp(browser.storage.local.get("setting"));
  55. // .then(
  56. // item => { response({"setting":item});
  57. // console.log("sending these items to contentscript:");
  58. // console.log(item);
  59. // browser.tabs.sendMessage(
  60. // sender.tab.id,
  61. // {"setting":JSON.stringify(item)}
  62. // )
  63. // });
  64. break;
  65. case "isOn":
  66. await null;
  67. resp(browser.storage.local.get("setting"));
  68. // .then(
  69. // item => {
  70. // let resp = "no"
  71. // if (item.setting.ison){resp = "yes";};
  72. // response(resp);
  73. // }
  74. // )
  75. default:
  76. break;
  77. }
  78. }