help.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { commonFetch } from '@/http/fetch'
  2. // 每4分钟调用一次,保持background一直有效
  3. export function PingPong() {
  4. chrome.tabs.query({}, (tabs = []) => {
  5. if (tabs.length) {
  6. let tab = tabs.filter((item) => { return item.active == true })
  7. if (tab.length) {
  8. chrome.tabs.sendMessage(tab[0].id, { actionType: 'BACK_PING' }, () => { });
  9. }
  10. }
  11. })
  12. }
  13. export function httpNetWork(funcName, data, sender) {
  14. return commonFetch(data)
  15. .then((response) => {
  16. chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_HTTP_RESPONSE', data: response, funcName });
  17. })
  18. .catch(() => {
  19. chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_HTTP_RESPONSE', data: null, funcName });
  20. })
  21. }
  22. // 向content 发送消息
  23. export const setContentMessage = (obj) => {
  24. chrome.tabs.query({}, (tabs = []) => {
  25. if (tabs.length) {
  26. tabs = tabs.filter((item) => { return item.active && item.selected && item.highlighted }) || []
  27. tabs.forEach((item) => {
  28. chrome.tabs.sendMessage(item.id, obj);
  29. })
  30. }
  31. })
  32. }
  33. export const httpContentToBack = (req, sender) => {
  34. commonFetch(req.data)
  35. .then((response) => {
  36. chrome.tabs.sendMessage(sender.tab.id, { actionType: 'HTTP_BACK_TO_CONTENT', data: response, callback_id: req.callback_id });
  37. })
  38. .catch(() => {
  39. chrome.tabs.sendMessage(sender.tab.id, { actionType: 'HTTP_BACK_TO_CONTENT', data: null, callback_id: req.callback_id });
  40. })
  41. }