utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 从服务端请求到的数据
  2. let state = null
  3. // opca / ocpx 下单为ocpx 其它为ocpa
  4. let priceType = 'ocpa'
  5. ~function(exports) {
  6. let priceElement = null // 出价input
  7. let priceTypeButtons = [] // 出价切换button
  8. let optimizeRadioGroup = null // 优化目标组
  9. function onPriceElementInput(e) {
  10. if (!state) return
  11. const max = +state.wx[priceType]
  12. if (+e.target.value > max) {
  13. e.target.value = max
  14. }
  15. }
  16. // 优化目标点击, 获取出价类型, 在选完出价类型(CPM或智能优化)后才能触发
  17. function onOptimizeRadioGroupClick(e) {
  18. setTimeout(() => {
  19. priceElement && priceElement.removeEventListener('input', onPriceElementInput)
  20. priceElement = document.getElementById('price_input')
  21. priceElement.addEventListener('input', onPriceElementInput)
  22. priceType = priceElement.parentElement.nextSibling.textContent.includes('下单') ? 'ocpx' : 'ocpa'
  23. })
  24. }
  25. // 出价方式类型点击
  26. function onPriceTypeButtonClick(e) {
  27. // 如果选中的是智能优化投放方式则获取优化目标 radio元素
  28. optimizeRadioGroup && optimizeRadioGroup.removeEventListener('click', onOptimizeRadioGroupClick)
  29. setTimeout(() => {
  30. let act = document.querySelector('.adui-button-active')
  31. if (act.textContent !== 'CPM') {
  32. // 智能优化
  33. optimizeRadioGroup = document.querySelectorAll('.adui-radio-group')[1]
  34. optimizeRadioGroup && optimizeRadioGroup.addEventListener('click', onOptimizeRadioGroupClick)
  35. } else {
  36. // CPM
  37. optimizeRadioGroup = null
  38. }
  39. })
  40. }
  41. exports.requestLimits = function() {
  42. !state && chrome.runtime.sendMessage(
  43. {
  44. type: "request",
  45. payload: {
  46. url: "http://baiyun.kwnpv.com/admin/advertise/throw_price_config/getAllPrice",
  47. method: "get"
  48. }
  49. },
  50. res => state = res
  51. )
  52. }
  53. exports.pagechange = function() {
  54. if (priceElement) {
  55. priceElement.removeEventListener('input', onPriceElementInput)
  56. }
  57. if (priceTypeButtons.length) {
  58. priceTypeButtons.forEach(button => button.removeEventListener('click', onPriceTypeButtonClick))
  59. }
  60. setTimeout(() => {
  61. // 出价输入框
  62. priceElement = document.getElementById('price_input')
  63. // 所有出价类型按钮
  64. priceTypeButtons = document.querySelectorAll('.adui-button-group button')
  65. // 该输入框只是刚进页面时的输入框, 在切换方式的时候应该重新获取
  66. if (priceElement) {
  67. priceType= priceElement.parentElement.nextSibling.textContent.includes('下单') ? 'ocpx' : 'ocpa'
  68. priceElement.addEventListener('input', onPriceElementInput)
  69. }
  70. if (priceTypeButtons.length) {
  71. onPriceTypeButtonClick()
  72. priceTypeButtons.forEach(button => {
  73. button.addEventListener('click', onPriceTypeButtonClick)
  74. })
  75. }
  76. }, 500)
  77. }
  78. const listenBackgroundTactics = {
  79. getCookie(payload, sendRes) {
  80. sendRes(document.cookie)
  81. }
  82. }
  83. exports.listenBackground = function() {
  84. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  85. const handler = listenBackgroundTactics[message.type]
  86. handler && handler(message.payload, sendResponse, sender)
  87. return true
  88. })
  89. }
  90. }(window)