| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // 从服务端请求到的数据
- let state = null
- // opca / ocpx 下单为ocpx 其它为ocpa
- let priceType = 'ocpa'
- ~function(exports) {
- let priceElement = null // 出价input
- let priceTypeButtons = [] // 出价切换button
- let optimizeRadioGroup = null // 优化目标组
- function onPriceElementInput(e) {
- if (!state) return
- const max = +state.wx[priceType]
- if (+e.target.value > max) {
- e.target.value = max
- }
- }
- // 优化目标点击, 获取出价类型, 在选完出价类型(CPM或智能优化)后才能触发
- function onOptimizeRadioGroupClick(e) {
- setTimeout(() => {
- priceElement && priceElement.removeEventListener('input', onPriceElementInput)
- priceElement = document.getElementById('price_input')
- priceElement.addEventListener('input', onPriceElementInput)
- priceType = priceElement.parentElement.nextSibling.textContent.includes('下单') ? 'ocpx' : 'ocpa'
- })
- }
- // 出价方式类型点击
- function onPriceTypeButtonClick(e) {
- // 如果选中的是智能优化投放方式则获取优化目标 radio元素
- optimizeRadioGroup && optimizeRadioGroup.removeEventListener('click', onOptimizeRadioGroupClick)
- setTimeout(() => {
- let act = document.querySelector('.adui-button-active')
- if (act.textContent !== 'CPM') {
- // 智能优化
- optimizeRadioGroup = document.querySelectorAll('.adui-radio-group')[1]
- optimizeRadioGroup && optimizeRadioGroup.addEventListener('click', onOptimizeRadioGroupClick)
- } else {
- // CPM
- optimizeRadioGroup = null
- }
- })
- }
- exports.requestLimits = function() {
- !state && chrome.runtime.sendMessage(
- {
- type: "request",
- payload: {
- url: "http://baiyun.kwnpv.com/admin/advertise/throw_price_config/getAllPrice",
- method: "get"
- }
- },
- res => state = res
- )
- }
-
- exports.pagechange = function() {
- if (priceElement) {
- priceElement.removeEventListener('input', onPriceElementInput)
- }
- if (priceTypeButtons.length) {
- priceTypeButtons.forEach(button => button.removeEventListener('click', onPriceTypeButtonClick))
- }
- setTimeout(() => {
- // 出价输入框
- priceElement = document.getElementById('price_input')
- // 所有出价类型按钮
- priceTypeButtons = document.querySelectorAll('.adui-button-group button')
- // 该输入框只是刚进页面时的输入框, 在切换方式的时候应该重新获取
- if (priceElement) {
- priceType= priceElement.parentElement.nextSibling.textContent.includes('下单') ? 'ocpx' : 'ocpa'
- priceElement.addEventListener('input', onPriceElementInput)
- }
- if (priceTypeButtons.length) {
- onPriceTypeButtonClick()
- priceTypeButtons.forEach(button => {
- button.addEventListener('click', onPriceTypeButtonClick)
- })
- }
- }, 500)
- }
- const listenBackgroundTactics = {
- getCookie(payload, sendRes) {
- sendRes(document.cookie)
- }
- }
- exports.listenBackground = function() {
- chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
- const handler = listenBackgroundTactics[message.type]
- handler && handler(message.payload, sendResponse, sender)
- return true
- })
- }
- }(window)
|