|
| 1 | +import { sharePostToGroup } from "./popup/helpers/facebook.js"; |
| 2 | +import { randInt } from "./popup/helpers/utils.js"; |
| 3 | + |
| 4 | +console.log("background"); |
| 5 | + |
| 6 | +const logs = []; |
| 7 | +const sharePostToGroups = [ |
| 8 | + // { |
| 9 | + // postUrl: "https://www.facebook.com/share/p/1phXqQ8TvxniRVsi/", |
| 10 | + // caption: "", |
| 11 | + // groups: [ |
| 12 | + // "https://www.facebook.com/groups/311528245988035", |
| 13 | + // "311528245988035", |
| 14 | + // ], |
| 15 | + // waitMin: 5000, |
| 16 | + // waitMax: 20000, |
| 17 | + // timer: "2024-09-08T21:06", |
| 18 | + // }, |
| 19 | +]; |
| 20 | + |
| 21 | +chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { |
| 22 | + if (request.action === "get_sharePostToGroups") { |
| 23 | + sendResponse(sharePostToGroups); |
| 24 | + } else if (request.action === "add_sharePostToGroups") { |
| 25 | + sharePostToGroups.push(request.data); |
| 26 | + sendResponse(sharePostToGroups); |
| 27 | + } else if (request.action === "remove_sharePostToGroups") { |
| 28 | + sharePostToGroups.splice(request.data?.index, 1); |
| 29 | + sendResponse(sharePostToGroups); |
| 30 | + } else if (request.action === "get_logs") { |
| 31 | + sendResponse(logs); |
| 32 | + } else if (request.action === "clear_logs") { |
| 33 | + logs.length = 0; |
| 34 | + sendResponse(logs); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +function addLog(log) { |
| 39 | + logs.push({ |
| 40 | + time: Date.now(), |
| 41 | + log: log, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +setInterval(() => { |
| 46 | + for (let i = 0; i < sharePostToGroups.length; i++) { |
| 47 | + let job = sharePostToGroups[i]; |
| 48 | + |
| 49 | + if (job.done || job.sharing) continue; |
| 50 | + |
| 51 | + if (job.running) { |
| 52 | + // sharing |
| 53 | + if ( |
| 54 | + !job.nextShareTime || |
| 55 | + Date.now() > new Date(job.nextShareTime).getTime() |
| 56 | + ) { |
| 57 | + let groupUrl = job.groups[job.shareIndex]; |
| 58 | + job.sharing = true; |
| 59 | + sharePostToGroup({ |
| 60 | + postUrl: job.postUrl, |
| 61 | + groupUrl: groupUrl, |
| 62 | + note: job.caption || "", |
| 63 | + }) |
| 64 | + .then((sharedUrl) => { |
| 65 | + console.log("share", groupUrl, job, sharedUrl); |
| 66 | + job.sharedUrls[job.shareIndex] = sharedUrl; |
| 67 | + addLog( |
| 68 | + `SHARE ${job.shareIndex + 1}/${job.groups.length}: shared ${ |
| 69 | + job.postUrl |
| 70 | + } to ${groupUrl} => ${sharedUrl}` |
| 71 | + ); |
| 72 | + }) |
| 73 | + .catch((e) => { |
| 74 | + console.log(e); |
| 75 | + addLog( |
| 76 | + `FAIL SHARE ${job.shareIndex + 1}/${job.groups.length}: share ${ |
| 77 | + job.postUrl |
| 78 | + } to ${groupUrl} failed ${e.message}` |
| 79 | + ); |
| 80 | + }) |
| 81 | + .finally(() => { |
| 82 | + job.shareIndex++; |
| 83 | + // check done |
| 84 | + if (job.shareIndex > job.groups.length - 1) { |
| 85 | + job.running = false; |
| 86 | + job.done = Date.now(); |
| 87 | + addLog( |
| 88 | + `DONE: share ${job.postUrl} to ${job.groups.length} groups` |
| 89 | + ); |
| 90 | + } else { |
| 91 | + job.nextShareTime = |
| 92 | + Date.now() + randInt(job.waitMin, job.waitMax); |
| 93 | + } |
| 94 | + job.sharing = false; |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + // check start running |
| 102 | + if (Date.now() > new Date(job.timer).getTime()) { |
| 103 | + job.running = true; |
| 104 | + job.shareIndex = 0; |
| 105 | + job.sharedUrls = Array(job.groups.length).fill(""); |
| 106 | + addLog( |
| 107 | + `START: share ${job.postUrl} to ${ |
| 108 | + job.groups.length |
| 109 | + } groups: ${job.groups.join(", ")}` |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | +}, 1000); |
0 commit comments