Spaces:
Sleeping

File size: 15,845 Bytes
8106906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FLASPO WP投稿ツール</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:-apple-system,'Hiragino Sans',sans-serif;background:#f8fafc;color:#1e293b}
.hd{background:linear-gradient(135deg,#1e40af,#3b82f6);padding:16px 24px;color:#fff;display:flex;align-items:center;justify-content:space-between}
.hd h1{font-size:20px}.hd span{font-size:12px;opacity:.8}
.ct{max-width:960px;margin:0 auto;padding:20px}
.tabs{display:flex;gap:4px;margin-bottom:20px;border-bottom:2px solid #e2e8f0}
.tab{padding:10px 20px;font-size:14px;font-weight:600;cursor:pointer;border-radius:8px 8px 0 0;background:#e2e8f0;color:#64748b}
.tab.on{background:#3b82f6;color:#fff}
.pn{display:none;background:#fff;border-radius:12px;padding:24px;box-shadow:0 1px 3px rgba(0,0,0,.1)}
.pn.on{display:block}
.card{background:#fff;border-radius:12px;padding:16px 20px;margin-bottom:16px;box-shadow:0 1px 3px rgba(0,0,0,.1)}
label{font-size:12px;font-weight:600;color:#64748b;display:block;margin-bottom:4px}
input,select,textarea{width:100%;padding:8px 12px;border:1px solid #e2e8f0;border-radius:8px;font-size:14px;font-family:inherit}
input:focus,textarea:focus{outline:none;border-color:#3b82f6}
textarea{resize:vertical}
.btn{padding:10px 20px;border:none;border-radius:8px;font-size:14px;font-weight:700;cursor:pointer;font-family:inherit}
.bp{background:#3b82f6;color:#fff}.bp:hover{background:#2563eb}
.bs{background:#e2e8f0;color:#475569}.bs:hover{background:#cbd5e1}
.bg{background:#059669;color:#fff}
.btn:disabled{opacity:.5;cursor:not-allowed}
.fld{margin-bottom:16px}
.fr{display:flex;gap:12px}.fr>*{flex:1}
.ok{background:#ecfdf5;border:1px solid #a7f3d0;color:#065f46;padding:12px;border-radius:8px;margin:8px 0}
.er{background:#fef2f2;border:1px solid #fecaca;color:#991b1b;padding:12px;border-radius:8px;margin:8px 0}
.inf{background:#eff6ff;border:1px solid #bfdbfe;color:#1e40af;padding:12px;border-radius:8px;margin:8px 0}
.mt{text-align:center;padding:16px;background:#f8fafc;border-radius:8px}
.mt .v{font-size:28px;font-weight:800;color:#3b82f6}
.mt .l{font-size:12px;color:#64748b}
h2{font-size:18px;margin-bottom:12px;color:#1e3a5f}
h3{font-size:15px;margin:16px 0 8px;color:#475569}
.ft{text-align:center;padding:20px;font-size:12px;color:#94a3b8}
.hid{display:none}
.spinner{display:inline-block;width:16px;height:16px;border:2px solid #e2e8f0;border-top:2px solid #3b82f6;border-radius:50%;animation:spin 1s linear infinite;margin-right:8px;vertical-align:middle}
@keyframes spin{to{transform:rotate(360deg)}}
.pb{background:#e2e8f0;border-radius:8px;height:24px;overflow:hidden;margin:12px 0}
.pf{background:linear-gradient(90deg,#3b82f6,#60a5fa);height:100%;transition:width .3s;display:flex;align-items:center;justify-content:center;font-size:11px;color:#fff;font-weight:700}
.sec{border:1px solid #e2e8f0;border-radius:10px;padding:16px;margin-bottom:12px;background:#fafbfc}
.sec-head{display:flex;align-items:center;gap:8px;margin-bottom:8px}
.sec-num{background:#3b82f6;color:#fff;width:28px;height:28px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:700;flex-shrink:0}
.sec input{font-weight:700;font-size:15px;border-color:#bfdbfe}
.sec textarea{font-size:13px;line-height:1.7}
.charcount{font-size:11px;color:#94a3b8;text-align:right;margin-top:2px}
.preview-box{background:#fff;border:1px solid #e2e8f0;border-radius:8px;padding:20px;margin-top:16px;font-size:14px;line-height:1.8}
.preview-box h2{font-size:20px;margin:20px 0 8px;padding-bottom:4px;border-bottom:2px solid #e2e8f0;color:#1e293b}
.preview-box p{margin:8px 0;color:#475569}
</style>
</head>
<body>
<div class="hd"><h1>FLASPO WordPress自動投稿ツール</h1><span>APIキー不要・見出し構造で記事を投稿</span></div>
<div class="ct">

<div class="card">
<h2>WordPress接続設定</h2>
<div class="fr">
<div><label>WP URL</label><input id="wu" value="https://magazine.flaspo.jp"></div>
<div><label>ユーザー名</label><input id="wn" value="flaspobot"></div>
<div><label>アプリケーションパスワード</label><input id="wp" type="password" value="2rAZ oFst 26Bd QqYn CR3H CW29"></div>
</div>
<div class="fr" style="margin-top:12px">
<div><label>Placid APIキー</label><input id="pk" type="password" value="placid-vrsirgutd5ro2uht-cssvlgcti5eyj8n4"></div>
<div><label>テンプレートID</label><input id="pt" value="4ezcji2mko7v1"></div>
</div>
<div style="margin-top:12px"><button class="btn bs" onclick="twp()">接続テスト</button><span id="ws"></span></div>
</div>

<div class="tabs">
<div class="tab on" onclick="stab(0)">1記事ずつ投稿</div>
<div class="tab" onclick="stab(1)">Excelで一括投稿</div>
</div>

<!-- Single Article -->
<div class="pn on" id="p0">
<h2>1記事ずつ投稿</h2>

<div class="fld"><label>記事タイトル</label><input id="s_title" placeholder="例: ふるさと納税の仕組みとは?図解でわかる税控除・やり方・メリット"></div>

<div class="fr">
<div class="fld"><label>カテゴリ</label><input id="s_cat" placeholder="例: 地方創生・社会課題"></div>
<div class="fld"><label>タグ(カンマ区切り)</label><input id="s_tags" placeholder="例: ふるさと納税, 地方創生"></div>
</div>
<div class="fr">
<div class="fld"><label>メタディスクリプション</label><input id="s_meta" placeholder="80〜120文字"></div>
<div class="fld"><label>スラッグ</label><input id="s_slug" placeholder="例: furusato-nouzei"></div>
</div>
<div class="fld"><label>フォーカスキーワード</label><input id="s_kw" placeholder="例: ふるさと納税 仕組み"></div>

<h3>記事の見出し構成(H2 × 4〜5本 + 各600文字)</h3>
<p style="color:#64748b;font-size:13px;margin-bottom:12px">Claudeなどで生成したテキストを見出しごとに貼り付けてください。</p>

<div id="sections"></div>
<button class="btn bs" onclick="addSec()" id="addBtn" style="margin-bottom:16px">+ 見出しを追加</button>

<div class="fr" style="gap:8px">
<button class="btn bp" id="s_btn" onclick="postSingle()" style="font-size:16px;padding:14px 28px">下書きとして投稿</button>
<button class="btn bs" onclick="showPreview()">プレビュー</button>
</div>
<div id="s_result" class="hid" style="margin-top:12px"></div>
<div id="s_preview" class="hid" style="margin-top:12px"></div>
</div>

<!-- Batch -->
<div class="pn" id="p1">
<h2>Excelで一括投稿</h2>
<p style="margin-bottom:16px;color:#64748b">記事一覧のExcelをアップロードして一括投稿。全て「下書き」として保存。</p>

<div class="inf" style="margin-bottom:16px">
<b>Excelの列構成:</b><br>
<span style="color:#3b82f6">タイトル / カテゴリ / タグ / メタディスクリプション / スラッグ / キーワード</span> +
<span style="color:#059669">見出し1 / 本文1 / 見出し2 / 本文2 / ... / 見出し5 / 本文5</span><br>
<a href="/api/template" style="color:#1e40af;font-weight:700">テンプレート(記入例付き)をダウンロード</a>
</div>

<div class="fld"><label>Excelファイル</label><input type="file" id="b_file" accept=".xlsx"></div>
<div id="b_info" class="hid"></div>
<button class="btn bp" id="b_btn" onclick="postBatch()" style="font-size:16px;padding:14px 28px" disabled>一括投稿を実行</button>
<div id="b_progress" class="hid"><div class="pb"><div class="pf" id="b_pf" style="width:0%">0%</div></div><div id="b_pt" style="font-size:13px;color:#64748b"></div></div>
<div id="b_result" class="hid" style="margin-top:12px"></div>
</div>

</div>
<div class="ft">FLASPO WordPress自動投稿ツール v2.0</div>

<script>
var secCount=0;
function stab(n){document.querySelectorAll('.tab').forEach(function(t,i){t.classList.toggle('on',i===n)});document.querySelectorAll('.pn').forEach(function(p,i){p.classList.toggle('on',i===n)})}
function sh(id){document.getElementById(id).classList.remove('hid')}
function cfg(){return{wp_url:document.getElementById('wu').value,wp_user:document.getElementById('wn').value,wp_pass:document.getElementById('wp').value,pk:document.getElementById('pk').value,pt:document.getElementById('pt').value}}

async function postJSON(url,body){
  var r=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body),credentials:'include'});
  return r.json();
}
async function poll(jobId,onProgress){
  while(true){
    await new Promise(function(r){setTimeout(r,2000)});
    try{
      var r=await fetch('/api/status/'+jobId,{credentials:'include'});
      var d=await r.json();
      if(onProgress) onProgress(d);
      if(d.status==='done') return {ok:true,result:d.result};
      if(d.status==='error') return {ok:false,error:d.result};
    }catch(e){return {ok:false,error:e.message}}
  }
}

function addSec(){
  if(secCount>=5){alert('見出しは最大5つです');return}
  secCount++;
  var n=secCount;
  var div=document.createElement('div');div.className='sec';div.id='sec'+n;
  div.innerHTML='<div class="sec-head"><span class="sec-num">'+n+'</span><input id="h'+n+'" placeholder="H2見出しを入力(例: ふるさと納税とは?制度の概要を解説)" style="flex:1"><button class="btn bs" style="padding:4px 10px;font-size:12px" onclick="rmSec('+n+')">削除</button></div><textarea id="b'+n+'" rows="8" placeholder="この見出しの本文を入力(約600文字)&#10;&#10;Claudeで生成したテキストをここに貼り付けてください。"></textarea><div class="charcount" id="cc'+n+'">0文字</div>';
  document.getElementById('sections').appendChild(div);
  document.getElementById('b'+n).addEventListener('input',function(){
    document.getElementById('cc'+n).textContent=this.value.length+'文字';
  });
  if(secCount>=5) document.getElementById('addBtn').style.display='none';
}
function rmSec(n){
  var el=document.getElementById('sec'+n);
  if(el) el.remove();
  secCount--;
  if(secCount<5) document.getElementById('addBtn').style.display='';
}

// Initialize with 4 sections
for(var i=0;i<4;i++) addSec();

async function twp(){
  var c=cfg();
  try{
    var r=await postJSON('/api/testwp',c);
    document.getElementById('ws').innerHTML=r.ok?'<span style="color:#059669;font-weight:700"> ✅ '+r.name+'</span>':'<span style="color:#dc2626;font-weight:700"> ❌ 失敗</span>';
  }catch(e){document.getElementById('ws').innerHTML='<span style="color:#dc2626"> 通信エラー</span>'}
}

function getSections(){
  var body={};
  for(var i=1;i<=5;i++){
    var hEl=document.getElementById('h'+i);
    var bEl=document.getElementById('b'+i);
    if(hEl) body['h'+i]=hEl.value;
    if(bEl) body['b'+i]=bEl.value;
  }
  return body;
}

async function showPreview(){
  var secs=getSections();
  try{
    var r=await postJSON('/api/preview',secs);
    if(r.ok){
      sh('s_preview');
      var display=r.html.replace(/<!-- wp:heading -->/g,'').replace(/<!-- \/wp:heading -->/g,'')
        .replace(/<!-- wp:paragraph -->/g,'').replace(/<!-- \/wp:paragraph -->/g,'')
        .replace(/class="wp-block-heading"/g,'');
      document.getElementById('s_preview').innerHTML='<h3>プレビュー('+r.chars+'文字)</h3><div class="preview-box">'+display+'</div>';
    }
  }catch(e){}
}

async function postSingle(){
  var c=cfg();
  var title=document.getElementById('s_title').value;
  if(!title){alert('タイトルを入力');return}
  var hasContent=false;
  for(var i=1;i<=5;i++){var h=document.getElementById('h'+i);if(h&&h.value){hasContent=true;break}}
  if(!hasContent){alert('見出しを1つ以上入力');return}

  var btn=document.getElementById('s_btn');
  btn.disabled=true;btn.innerHTML='<span class="spinner"></span>投稿中...';
  sh('s_result');document.getElementById('s_result').innerHTML='<div class="inf"><span class="spinner"></span> WordPressに投稿中...</div>';
  try{
    var body={...c,title:title,category:document.getElementById('s_cat').value,
      tags:document.getElementById('s_tags').value,meta:document.getElementById('s_meta').value,
      slug:document.getElementById('s_slug').value,keyword:document.getElementById('s_kw').value,
      ...getSections()};
    var start=await postJSON('/api/post-single',body);
    var result=await poll(start.job_id);
    if(result.ok&&result.result.status==='ok'){
      document.getElementById('s_result').innerHTML='<div class="ok"><b>✅ 投稿成功!</b><br><a href="'+result.result.url+'" target="_blank" style="color:#059669;font-weight:700">WordPress編集画面を開く →</a></div>';
    }else{
      document.getElementById('s_result').innerHTML='<div class="er">❌ '+(result.result.error||result.error||'失敗')+'</div>';
    }
  }catch(e){document.getElementById('s_result').innerHTML='<div class="er">エラー: '+e.message+'</div>'}
  btn.disabled=false;btn.textContent='下書きとして投稿';
}

var batchPath='';
document.getElementById('b_file').addEventListener('change',async function(){
  var f=this.files[0];if(!f)return;
  try{
    var fd=new FormData();fd.append('file',f);
    var r=await(await fetch('/api/upload-batch',{method:'POST',body:fd,credentials:'include'})).json();
    if(r.ok){
      batchPath=r.path;sh('b_info');
      var html='<div class="mt" style="margin:12px 0"><div class="v">'+r.count+'</div><div class="l">記事数</div></div>';
      html+='<div style="max-height:200px;overflow-y:auto;font-size:13px">';
      r.articles.forEach(function(a,i){html+='<div style="padding:4px 0;border-bottom:1px solid #f1f5f9"><b>['+(i+1)+']</b> '+a.title+' <span style="color:#64748b">('+a.category+' / 見出し'+a.sections+'本)</span></div>'});
      html+='</div>';
      document.getElementById('b_info').innerHTML=html;
      document.getElementById('b_btn').disabled=false;
    }
  }catch(e){console.error(e)}
});

async function postBatch(){
  var c=cfg();
  if(!batchPath){alert('Excelをアップロード');return}
  var btn=document.getElementById('b_btn');
  btn.disabled=true;btn.innerHTML='<span class="spinner"></span>投稿中...';
  sh('b_progress');sh('b_result');
  document.getElementById('b_result').innerHTML='';
  try{
    var start=await postJSON('/api/post-batch',{...c,excel_path:batchPath});
    var result=await poll(start.job_id,function(d){
      if(d.total>0){
        var pct=Math.round(d.progress/d.total*100);
        document.getElementById('b_pf').style.width=pct+'%';
        document.getElementById('b_pf').textContent=pct+'%';
        document.getElementById('b_pt').textContent='['+d.progress+'/'+d.total+'] '+d.current;
      }
    });
    if(result.ok&&result.result.ok){
      var ok=result.result.results.filter(function(x){return x.status==='ok'}).length;
      var fail=result.result.results.length-ok;
      document.getElementById('b_pf').style.width='100%';document.getElementById('b_pf').textContent='100%';
      var h='<div class="ok" style="font-size:16px;font-weight:700">完了! ✅'+ok+'件成功 / ❌'+fail+'件失敗</div>';
      result.result.results.forEach(function(x){
        if(x.status==='ok'){h+='<div style="padding:4px 0;border-bottom:1px solid #f1f5f9">✅ <b>'+x.title+'</b> <a href="'+x.url+'" target="_blank">編集→</a></div>'}
        else{h+='<div style="padding:4px 0;border-bottom:1px solid #f1f5f9">❌ <b>'+x.title+'</b> '+(x.error||'')+'</div>'}
      });
      document.getElementById('b_result').innerHTML=h;
    }else{document.getElementById('b_result').innerHTML='<div class="er">'+(result.error||result.result.error||'エラー')+'</div>'}
  }catch(e){document.getElementById('b_result').innerHTML='<div class="er">エラー: '+e.message+'</div>'}
  btn.disabled=false;btn.textContent='一括投稿を実行';
}
</script>
</body>
</html>