您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
添加批量开启/暂停/删除功能
当前为
// ==UserScript== // @name 微信广告后台增强 // @namespace http://tampermonkey.net/ // @version 0.4 // @description 添加批量开启/暂停/删除功能 // @author You // @require https://unpkg.com/[email protected]/dist/jquery.min.js // @require https://unpkg.com/[email protected]/dist/async.min.js // @match https://mp.weixin.qq.com/promotion/* // @grant GM_addStyle // ==/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(`<input class="idSelector" name="selectedId" value="${i}" type="checkbox" />`); }); } const addProgressBar = ({ total = 0 }) => { const bar = `<div id="progressbar"> <span id="finished">0</span> / <span id="total">${total}</span> </div>`; $('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 = $('<button class="pauseBtn Button_new__base-1H7bt Button_new__default-3C02p Button_new__mini-catyW"><svg width="10" height="10" viewBox="0 0 14 14" style="margin-right: 3px;"><circle cx="7" cy="7" r="7"></circle><path d="M4 4h2v6H4V4zm4 0h2v6H8V4z" fill="#fff"></path></svg> 暂停所选</button>'); 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 = $(`<button class="startBtn Button_new__base-1H7bt Button_new__default-3C02p Button_new__mini-catyW"> <svg width="10" height="10" style="margin-right: 3px;"><path d="M5 10A5 5 0 1 1 5 0a5 5 0 0 1 0 10zM3.5 2.5v5l4-2.5-4-2.5z" fill-rule="nonzero"></path></svg> 开启投放</button>`); 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 = $('<button class="delBtn Button_new__base-1H7bt Button_new__default-3C02p Button_new__mini-catyW"><svg width="10" height="10" viewBox="0 0 14 14" style="margin-right: 3px;"><circle cx="7" cy="7" r="7"></circle><path d="M4 4h2v6H4V4zm4 0h2v6H8V4z" fill="#fff"></path></svg> 删除所选</button>'); 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 = $('<label class="selAllLabel"><input type="checkbox" class="selAll" /> 全选/取消</label>'); 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 = $('<button class="addBtn Button_new__base-1H7bt Button_new__primary-2diSJ Button_new__mini-catyW Button_new__iconBtn-NYPRT">批量操作</button>'); 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();