SPB Git

Repository Public

fb_post_img.sh 7.0 KB 219 lines shellscript

1#!/bin/bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : FB Ka Automation8# File             : posts/fb_post_img.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 texte + image sur la page Facebook Groupe KA (Safari, window id $WIN)41set -u42FILE="$1"43IMG="$2"44WIN="${3:-13939}"4546js() {47  osascript <<EOF48tell application "Safari"49  do JavaScript "$1" in tab 1 of window id $WIN50end tell51EOF52}5354clickbtn() {55  js "56(function(){57  var dlgs=document.querySelectorAll('div[role=dialog]');58  if(!dlgs.length) return 'NO_DIALOG';59  var btn=null;60  dlgs.forEach(function(dlg){61    dlg.querySelectorAll('[role=button]').forEach(function(b){62      var al=b.getAttribute('aria-label')||'';63      if(al==='$1') btn=b;64    });65  });66  if(!btn) return 'NO_BTN';67  if(btn.getAttribute('aria-disabled')==='true') return 'DISABLED';68  ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});69  return 'clicked';70})();"71}7273# Étape 0 : recharger l'accueil de la page74osascript -e "tell application \"Safari\" to set URL of tab 1 of window id $WIN to \"https://www.facebook.com/profile.php?id=61593422723708\""75sleep 107677# Étape 1 : ouvrir le composeur (attendre le rendu, jusqu'à ~45 s)78R1="NOT_FOUND"79for i in 1 2 3 4 5 6 7 8 9; do80  R1=$(js "81(function(){82  var target=null;83  document.querySelectorAll('span,div').forEach(function(e){84    var t=(e.innerText||'').trim();85    if(!target && e.children.length===0 && (t.indexOf('Présentez votre marque')===0 || t.indexOf('Que voulez-vous dire')===0)) target=e;86  });87  if(!target) return 'NOT_FOUND';88  var btn=target.closest('[role=button]');89  if(!btn) return 'NO_BTN_ANCESTOR';90  ['mousedown','mouseup','click'].forEach(function(ev){91    btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));92  });93  return 'opened';94})();")95  echo "step1 (essai $i): $R1"96  [ "$R1" = "opened" ] && break97  sleep 598done99[ "$R1" = "opened" ] || exit 1100101# Étape 2 : coller le texte102R2a="NO_DIALOG"103for i in 1 2 3 4 5 6 7 8; do104  R2a=$(js "105(function(){106  var dlg=document.querySelector('div[role=dialog]');107  if(!dlg) return 'NO_DIALOG';108  var box=dlg.querySelector('div[contenteditable=true][role=textbox]');109  if(!box) return 'NO_BOX';110  box.focus();111  return 'focused';112})();")113  echo "step2a (essai $i): $R2a"114  [ "$R2a" = "focused" ] && break115  sleep 3116done117[ "$R2a" = "focused" ] || exit 1118sleep 1119# Collage 100 % JS (ClipboardEvent synthétique) — insensible au focus/clavier.120# Lexical traite le paste en asynchrone : vérifier la longueur APRÈS un délai.121B64=$(base64 -i "$FILE" | tr -d '\n')122LEN=0123for i in 1 2 3; do124  js "125(function(){126  var dlg=document.querySelector('div[role=dialog]');127  if(!dlg) return 'NO_DIALOG';128  var box=dlg.querySelector('div[contenteditable=true][role=textbox]');129  if(!box) return 'NO_BOX';130  box.focus();131  var txt=decodeURIComponent(escape(atob('$B64')));132  var dt=new DataTransfer();133  dt.setData('text/plain', txt);134  box.dispatchEvent(new ClipboardEvent('paste',{clipboardData:dt,bubbles:true,cancelable:true}));135  return 'pasted';136})();" >/dev/null137  sleep 3138  R2b=$(js "139(function(){140  var dlg=document.querySelector('div[role=dialog]');141  if(!dlg) return 'NO_DIALOG';142  var box=dlg.querySelector('div[contenteditable=true][role=textbox]');143  return 'len:'+(box?box.innerText.length:-1);144})();")145  echo "step2b (essai $i): $R2b"146  LEN=${R2b#len:}147  [ "$LEN" -gt 100 ] 2>/dev/null && break148  sleep 2149done150[ "$LEN" -gt 100 ] 2>/dev/null || { echo "paste failed"; exit 1; }151sleep 2152153# Étape 3 : joindre l'image (bouton Photo/Vidéo -> panneau fichier)154R3=$(clickbtn "Photo/Vidéo")155echo "step3 (Photo/Vidéo): $R3"156[ "$R3" = "clicked" ] || exit 1157sleep 3158# si aucun panneau fichier, cliquer la zone « Ajouter des photos/vidéos »159SHEETS=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)160if [ "$SHEETS" = "0" ]; then161  R3b=$(js "162(function(){163  var btn=null;164  document.querySelectorAll('div[role=dialog] [role=button]').forEach(function(b){165    var t=(b.innerText||'').trim();166    if(/Ajouter des photos|Ajouter photos|photos\\/vidéos/i.test(t) && !btn) btn=b;167  });168  if(!btn) return 'NO_DROPZONE';169  ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});170  return 'dropzone_clicked';171})();")172  echo "step3b: $R3b"173  sleep 3174  SHEETS=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)175fi176echo "sheets=$SHEETS"177[ "$SHEETS" = "1" ] || { echo "no file panel"; exit 1; }178osascript <<EOF179tell application "Safari" to activate180delay 0.5181tell application "System Events"182  tell process "Safari"183    keystroke "g" using {command down, shift down}184    delay 1.2185    keystroke "$IMG"186    delay 0.8187    key code 36188    delay 1.5189    key code 36190  end tell191end tell192EOF193echo "file selected, upload en cours…"194sleep 10195196# Étape 4 : Suivant puis Publier197R4=$(clickbtn "Suivant")198echo "step4 (Suivant): $R4"199if [ "$R4" = "clicked" ]; then sleep 8; fi200R5="NO_BTN"201for i in 1 2 3 4 5; do202  R5=$(clickbtn "Publier")203  echo "step5 (Publier, essai $i): $R5"204  [ "$R5" = "clicked" ] && break205  sleep 4206done207[ "$R5" = "clicked" ] || exit 1208209# Étape 6 : attendre la fermeture (upload d'image = lent, jusqu'à ~90 s)210R6="STILL_OPEN"211for i in 1 2 3 4 5 6 7 8 9 10 11 12; do212  sleep 8213  R6=$(js "document.querySelector('div[role=dialog]') ? 'STILL_OPEN' : 'CLOSED';")214  echo "step6 (essai $i): $R6"215  [ "$R6" = "CLOSED" ] && break216done217[ "$R6" = "CLOSED" ] || exit 1218echo "OK: $(basename "$FILE") + $(basename "$IMG") publié"219
Files main
Menu

Theme