background.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var database;
  2. function send_data(msg){
  3. chrome.storage.local.get(['database_host'], function(result) {
  4. if (result.database_host) {
  5. console.log('database_host currently is ' + result.database_host);
  6. database = "http://"+result.database_host+":7866/endpoint";
  7. } else {
  8. database = "http://127.0.0.1:7866/endpoint";
  9. }
  10. fetch(database, {
  11. method: "POST",
  12. headers: {
  13. "Content-Type": "application/json",
  14. },
  15. body: JSON.stringify(msg),
  16. })
  17. .then((response) => response.json())
  18. .then((data) => {
  19. console.log(data.result);
  20. });
  21. });
  22. }
  23. chrome.runtime.onMessage.addListener(async (msg, sender) => {
  24. if (msg.flag == "open_tab_and_cache_from_content"){
  25. var url = "";
  26. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  27. url = tabs[0].url;
  28. console.log(url);
  29. if (msg.data) {
  30. chrome.storage.sync.get(['data'], function(result) {
  31. chrome.storage.sync.set({ data: result.data }, function() {
  32. send_data({ 'content' : msg.data, 'query': '', 'url':url, 'task':'cache', 'type':msg.type});
  33. });
  34. });
  35. }
  36. });
  37. }
  38. if (msg.flag == "open_popup_and_send_url_from_popup"){
  39. if (msg.data) {
  40. chrome.storage.sync.get(['data'], function(result) {
  41. chrome.storage.sync.set({ data: result.data }, function() {
  42. send_data({ 'url' : msg.data, 'task':'pop_url'});
  43. });
  44. });
  45. }
  46. }
  47. if (msg.flag == "set_addr"){
  48. if (msg.data) {
  49. chrome.storage.sync.get(['data'], function(result) {
  50. chrome.storage.sync.set({ data: result.data }, function() {
  51. send_data({ 'addr' : msg.data, 'task':'set_addr'});
  52. });
  53. });
  54. }
  55. }
  56. });