// ==UserScript== // @name 微信广告后台增强 // @namespace http://tampermonkey.net/ // @version 0.4 // @description 添加批量开启/暂停/删除功能 // @author You // @require https://unpkg.com/jquery@3.3.1/dist/jquery.min.js // @require https://unpkg.com/async@2.6.1/dist/async.min.js // @match https://mp.weixin.qq.com/promotion/* // @grant GM_addStyle // @downloadURL none // ==/UserScript== 'use strict'; var initialized = false; var parseQueryString = function( queryString ) { var params = {}, queries, temp, i, l; // Split into key/value pairs queries = queryString.split("&"); // Convert the array of strings into an object for ( i = 0, l = queries.length; i < l; i++ ) { temp = queries[i].split('='); params[temp[0]] = temp[1]; } return params; }; const getToken = () => { const href = document.location.href; const [ url, token ] = href.match(/token=(\d+)/) return token; } const addCheck = () => { $('main .Table_new__table-3FAn2 tbody tr').each((i, tr) => { const firstCell = $(tr).find('td:first'); const txt = firstCell.text(); // 单元格内容举例:ale1017-2-副本计划 ID: 77405915 const [, id] = txt.split('计划 ID: ') firstCell.append(``); }); } const addProgressBar = ({ total = 0 }) => { const bar = `
0 / ${total}
`; $('body').append(bar); }; const incrProgressBar = (count=1) => { const current = parseInt($('#finished').html()); $('#finished').html(current + count); }; const removeProgressBar = () => { $('#progressbar').remove(); } const addGlobalStyle = (link) => { var head, tag; head = document.getElementsByTagName('head')[0]; if (!head) { return; } tag = document.createElement('link'); tag.rel = 'stylesheet'; tag.href = link; head.appendChild(tag); } const getSelectedIdAll = () => { const results = []; $('.idSelector:checked').each(function(){ const firstCell = $(this).parent('td'); const txt = firstCell.text(); const [, id] = txt.split('计划 ID: '); results.push(parseInt(id)); }); return results; }; const getCurrentPos = () => { const qs = parseQueryString(document.location.search); // 公众号广告是0,pos_type 是 0;朋友圈广告是1,pos_type 是 999 if(parseInt(qs.type)) { return 999; } else { return 0; } } const request = ({ url, args }) => { const token = getToken(); return $.post( url, { args: JSON.stringify(args), token, appid: '', spid: '', _: (new Date).getTime() }); }; const delete_campaign = ({ cid, pos_type }) => { const args = { cid, pos_type }; return request({ url: 'https://mp.weixin.qq.com/promotion/v3/delete_campaign', args }); }; const resume_campaign = ({ cid, pos_type }) => { const args = { cid, pos_type }; return request({ url: 'https://mp.weixin.qq.com/promotion/v3/resume_campaign', args }); }; const suspend_campaign = ({ cid, pos_type }) => { const args = { cid, pos_type }; return request({ url: 'https://mp.weixin.qq.com/promotion/v3/suspend_campaign', args }); }; //批量暂停的按钮 const addPauseBtn = () => { // conitnue = '' // const pauseBtn = $(''); pauseBtn.click(() => { const idArr = getSelectedIdAll(); if (!confirm(`是否确认暂停?共 ${idArr.length} 个计划`)) { return; } const pos = getCurrentPos(); const total = idArr.length; addProgressBar({ total }); async.mapLimit(idArr, 1, (id, callback) => { setTimeout(()=>{ suspend_campaign({ cid: id, pos_type: pos }).done(res => { callback(null, res); }).fail(() => { callback(null, { ret: -1 }); }).always(() => { incrProgressBar(); }); }, 100); }, function(err, results) { removeProgressBar(); let success = 0; let failed = 0; for(let result of results) { if(result.ret === 0) { success++; } else { failed++; console.log(result); } } if(failed > 0) { alert(`提醒:失败数 ${failed},完成 ${success}`); } }); }); $('.ui-flex').prepend(pauseBtn); const startBtn = $(``); startBtn.click(() => { const idArr = getSelectedIdAll(); if (!confirm(`是否确认投放?共 ${idArr.length} 个计划`)) { return; } const pos = getCurrentPos(); const total = idArr.length; addProgressBar({ total }); async.mapLimit(idArr, 1, (id, callback) => { setTimeout(()=>{ resume_campaign({ cid: id, pos_type: pos }).done(res => { incrProgressBar(); callback(null, res); incrProgressBar(); }).fail(() => { callback(null, { ret: -1 }); }); }, 100); }, function(err, results) { removeProgressBar(); let success = 0; let failed = 0; for(let result of results) { if(result.ret === 0) { success++; } else { failed++; console.log(result); } } if(failed > 0) { alert(`提醒:失败数 ${failed},完成 ${success}`); } }); }); $('.ui-flex').prepend(startBtn); const delBtn = $(''); delBtn.click(() => { const idArr = getSelectedIdAll(); if (!confirm(`是否确认删除?共 ${idArr.length} 个计划`)) { return; } const pos = getCurrentPos(); const total = idArr.length; addProgressBar({ total }); async.mapLimit(idArr, 1, (id, callback) => { setTimeout(()=>{ delete_campaign({ cid: id, pos_type: pos }).done(res => { callback(null, res); }).fail(() => { callback(null, { ret: -1 }); }).always(() => { incrProgressBar(); }); }, 1000); }, function(err, results) { removeProgressBar(); let success = 0; let failed = 0; for(let result of results) { if(result.ret === 0) { success++; } else { failed++; console.log(result); } } if(failed > 0) { alert(`提醒:失败数 ${failed},完成 ${success}`); } }); }); $('.ui-flex').prepend(delBtn); const selectAll = $(''); selectAll.change(function(i) { var status = $(this).find('.selAll')[0].checked; $('.idSelector').each(function(){ this.checked = status; }); }); $('.ui-flex').prepend(selectAll); }; //开始操作的按钮 const addStartBtn = () => { const startBtn = $(''); startBtn.click(() => { if(initialized) { return; } initialized = true; addCheck(); addPauseBtn(); startBtn.remove(); }); $('body').append(startBtn); addPauseBtn(); }; //批量开始的按钮 GM_addStyle(` .pauseBtn {} .startBtn{ color: #67C23A !important; } .delBtn{ color:#F56C6C !important; } .pauseBtn, .startBtn, .delBtn { margin-right: 15px; } .selAllLabel { line-height: 1; padding:6px 12px; } .idSelector { position:absolute; left:5px; top:5px; height: 36px; font-size: 43px; } .addBtn { position: fixed; top:50%; left:0; z-index:2; } #progressbar{ z-index:2; position: fixed; top:30%; left:46%; padding:0 20px; border-radius: 12px; font-size:64px; background-color: hsla(0,87%,69%,.1); border-color: hsla(0,87%,69%,.2); color: #f56c6c; } .ui-flex{ display:block !important; min-height:80px; } #test_pagination{ float: right } `); addStartBtn();