help.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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, sendResponse) {
  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. let tab = tabs.filter((item) => { return item.active == true }) || []
  27. if (tab.length) {
  28. // 未读消息
  29. chrome.tabs.sendMessage(tab[0].id, obj);
  30. }
  31. }
  32. })
  33. }