Repository Public
1#!/bin/bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : FB Ka Automation8# File : posts/fb_post.sh9# Module : posts10# Repository : https://git.spboucher.ai/fb-ka-automation11# Created : 2026-12-0112# Last Modified : 2026-12-0113#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940# Publie le contenu d'un fichier texte sur la page Facebook ouverte dans Safari (window id $WIN)41set -u42FILE="$1"43WIN="${2:-13823}"4445js() {46 osascript <<EOF47tell application "Safari"48 do JavaScript "$1" in tab 1 of window id $WIN49end tell50EOF51}5253clickbtn() { # $1 = exact aria-label ou préfixe54 js "55(function(){56 var dlgs=document.querySelectorAll('div[role=dialog]');57 if(!dlgs.length) return 'NO_DIALOG';58 var btn=null;59 dlgs.forEach(function(dlg){60 dlg.querySelectorAll('[role=button]').forEach(function(b){61 var al=b.getAttribute('aria-label')||'';62 if(al==='$1') btn=b;63 });64 });65 if(!btn) return 'NO_BTN';66 if(btn.getAttribute('aria-disabled')==='true') return 'DISABLED';67 ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});68 return 'clicked';69})();"70}7172# Étape 0 : recharger l'accueil de la page73osascript -e "tell application \"Safari\" to set URL of tab 1 of window id $WIN to \"https://www.facebook.com/profile.php?id=61593422723708\""74sleep 107576# Étape 1 : ouvrir le composeur77R1=$(js "78(function(){79 var target=null;80 document.querySelectorAll('span,div').forEach(function(e){81 var t=(e.innerText||'').trim();82 if(!target && e.children.length===0 && (t.indexOf('Présentez votre marque')===0 || t.indexOf('Que voulez-vous dire')===0)) target=e;83 });84 if(!target) return 'NOT_FOUND';85 var btn=target.closest('[role=button]');86 if(!btn) return 'NO_BTN_ANCESTOR';87 ['mousedown','mouseup','click'].forEach(function(ev){88 btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));89 });90 return 'opened';91})();")92echo "step1: $R1"93[ "$R1" = "opened" ] || exit 194sleep 49596# Étape 2 : coller le texte via presse-papiers (attendre que le dialogue soit prêt)97R2a="NO_DIALOG"98for i in 1 2 3 4 5 6 7 8; do99 R2a=$(js "100(function(){101 var dlg=document.querySelector('div[role=dialog]');102 if(!dlg) return 'NO_DIALOG';103 var box=dlg.querySelector('div[contenteditable=true][role=textbox]');104 if(!box) return 'NO_BOX';105 box.focus();106 return 'focused';107})();")108 echo "step2a (essai $i): $R2a"109 [ "$R2a" = "focused" ] && break110 sleep 3111done112[ "$R2a" = "focused" ] || exit 1113sleep 1114# Collage 100 % JS (ClipboardEvent synthétique) — insensible au focus/clavier.115# Lexical traite le paste en asynchrone : vérifier la longueur APRÈS un délai.116B64=$(base64 -i "$FILE" | tr -d '\n')117LEN=0118for i in 1 2 3; do119 js "120(function(){121 var dlg=document.querySelector('div[role=dialog]');122 if(!dlg) return 'NO_DIALOG';123 var box=dlg.querySelector('div[contenteditable=true][role=textbox]');124 if(!box) return 'NO_BOX';125 box.focus();126 var txt=decodeURIComponent(escape(atob('$B64')));127 var dt=new DataTransfer();128 dt.setData('text/plain', txt);129 box.dispatchEvent(new ClipboardEvent('paste',{clipboardData:dt,bubbles:true,cancelable:true}));130 return 'pasted';131})();" >/dev/null132 sleep 3133 R2b=$(js "134(function(){135 var dlg=document.querySelector('div[role=dialog]');136 if(!dlg) return 'NO_DIALOG';137 var box=dlg.querySelector('div[contenteditable=true][role=textbox]');138 return 'len:'+(box?box.innerText.length:-1);139})();")140 echo "step2b (essai $i): $R2b"141 LEN=${R2b#len:}142 [ "$LEN" -gt 100 ] 2>/dev/null && break143 sleep 2144done145[ "$LEN" -gt 100 ] 2>/dev/null || { echo "paste failed"; exit 1; }146sleep 3147148# Étape 3 : Suivant (si présent) puis Publier149R3=$(clickbtn "Suivant")150echo "step3 (Suivant): $R3"151if [ "$R3" = "clicked" ]; then sleep 8; fi152R4="NO_BTN"153for i in 1 2 3 4 5; do154 R4=$(clickbtn "Publier")155 echo "step4 (Publier, essai $i): $R4"156 [ "$R4" = "clicked" ] && break157 sleep 4158done159[ "$R4" = "clicked" ] || exit 1160sleep 10161162# Étape 5 : vérifier que le dialogue est fermé163R5=$(js "document.querySelector('div[role=dialog]') ? 'STILL_OPEN' : 'CLOSED';")164echo "step5: $R5"165[ "$R5" = "CLOSED" ] || exit 1166echo "OK: $(basename "$FILE") publié"167