chromeExtension.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { pageUrl } from "@/http/configAPI.js"
  2. export const LANDING_PAGE = {
  3. name: 'received_log',
  4. url: pageUrl
  5. }
  6. export const LANDING_PAGE_MIND = {
  7. name: 'mid',
  8. url: pageUrl
  9. }
  10. export function setChromeStorage(params) {
  11. chrome.storage.local.set(params)
  12. }
  13. export function getChromeStorage(key = '', callback) {
  14. let params = {}
  15. params[key] = ''
  16. if (!callback) {
  17. return new Promise((resolve) => {
  18. chrome.storage.local.get(params, (item) => {
  19. if (item[key]) {
  20. resolve(JSON.parse(item[key]))
  21. } else {
  22. resolve(null)
  23. }
  24. });
  25. })
  26. }
  27. chrome.storage.local.get(params, (item) => {
  28. if (item[key]) {
  29. callback(JSON.parse(item[key]))
  30. } else {
  31. callback(null)
  32. }
  33. });
  34. }
  35. export function setChromeCookie({
  36. name,
  37. url
  38. }, value_obj) {
  39. chrome.cookies.set({
  40. expirationDate: new Date().getTime() / 10,
  41. name: name,
  42. url: url,
  43. value: encodeURIComponent(JSON.stringify(value_obj)) || ''
  44. })
  45. }
  46. export function getChromeCookie({
  47. name,
  48. url
  49. }, callback) {
  50. chrome.cookies.getAll(
  51. {
  52. name: name || '',
  53. url: url || ''
  54. }, (e = []) => {
  55. let _str = '[]'
  56. if (e.length > 0) {
  57. _str = e[0].value
  58. }
  59. let _arr = JSON.parse(decodeURIComponent(_str)) || []
  60. callback(_arr)
  61. }
  62. )
  63. }
  64. // 累加ChromeCookie
  65. export function concatChromeCookie({ name, url }, value_obj) {
  66. chrome.cookies.getAll(
  67. {
  68. name: name || '',
  69. url: url || ''
  70. }, (e = []) => {
  71. let _str = '[]'
  72. if (e.length > 0) {
  73. _str = e[0].value
  74. }
  75. let _arr = JSON.parse(decodeURIComponent(_str)) || []
  76. _arr = _arr.concat(value_obj)
  77. // 删除cookies
  78. chrome.cookies.remove(LANDING_PAGE, () => {
  79. chrome.cookies.set({
  80. expirationDate: new Date().getTime() / 10,
  81. name: name,
  82. url: url,
  83. value: encodeURIComponent(JSON.stringify(_arr)) || ''
  84. }, (e) => {
  85. console.log(e)
  86. })
  87. }
  88. )
  89. }
  90. )
  91. }
  92. export function removeChromeCookie(params, cb) {
  93. let {name, url} = params;
  94. chrome.cookies.remove({name, url}, () => {
  95. cb && cb
  96. })
  97. }