Snapshot of the working tree exactly as it stood, no edits. This is the predecessor
PRICEGOD was rewritten from ("kept intact, untouched" per pricegod/README.md), and it
is still the only place the DYMO scale is actually implemented —
rfid-daemon/index.js:1068-1265: HID discovery, parseScaleReport, one-shot read, and a
streaming /weight + /scale/start|stop session whose JSON shape PRICEGOD's daemon.js
already speaks.
Preserved verbatim on purpose (hence -og), including the known bug: DYMO_PIDS at
index.js:1082 is [0x8003, 0x8004], so it cannot see the bench M25 (0x8009). Fix that
in whatever daemon inherits the scale, not here.
node_modules stays ignored (22M of the 24M tree). No credentials in the import: the
two PEM markers in utils.js/sheets.js only strip headers off a key read from settings,
and mrpadmin / johnking are an SSH and a Postgres username, both key/trust auth.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
198 lines
7.4 KiB
JavaScript
198 lines
7.4 KiB
JavaScript
function saveWpSettings() {
|
|
const endpoint = document.getElementById('wpEndpoint').value;
|
|
const key = document.getElementById('wpKey').value;
|
|
const secret = document.getElementById('wpSecret').value;
|
|
|
|
const wpSettings = { endpoint, key, secret };
|
|
|
|
chrome.storage.local.set({ wpSettings }, () => {
|
|
// console.log('WordPress settings saved');
|
|
showStatusMessage('wpStatus', 'WordPress settings saved successfully!', true);
|
|
});
|
|
}
|
|
|
|
function loadWpSettings() {
|
|
chrome.storage.local.get(['wpSettings'], (result) => {
|
|
if (result.wpSettings) {
|
|
// console.log('Loading WordPress settings:', result.wpSettings);
|
|
document.getElementById('wpEndpoint').value = result.wpSettings.endpoint || '';
|
|
document.getElementById('wpKey').value = result.wpSettings.key || '';
|
|
document.getElementById('wpSecret').value = result.wpSettings.secret || '';
|
|
}
|
|
});
|
|
}
|
|
|
|
async function testWpConnection() {
|
|
console.log('--- Testing WordPress Connection ---');
|
|
try {
|
|
const settings = await new Promise(resolve => chrome.storage.local.get('wpSettings', data => resolve(data.wpSettings)));
|
|
|
|
if (!settings || !settings.endpoint) {
|
|
throw new Error('Endpoint URL is not configured.');
|
|
}
|
|
|
|
console.log('TESTING: Using endpoint:', settings.endpoint);
|
|
|
|
const response = await fetch(settings.endpoint, {
|
|
method: 'GET', // Use GET for a simple test
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
const responseText = await response.text();
|
|
|
|
if (response.ok) {
|
|
console.log('TEST SUCCESS: Server responded with:', responseText);
|
|
alert('Connection successful!');
|
|
} else {
|
|
console.error('TEST FAILED: Server responded with an error.', `Status: ${response.status}`, `Response: ${responseText}`);
|
|
throw new Error(`Connection failed: ${responseText}`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('TEST FATAL: An exception occurred during the connection test.', error);
|
|
alert(`Connection test failed: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async function sendToWordPress(releaseData) {
|
|
console.log('--- Starting WordPress Import ---');
|
|
|
|
if (!releaseData) {
|
|
console.error('WP ERROR: releaseData object is missing.');
|
|
alert('WP ERROR: releaseData object is missing.');
|
|
return;
|
|
}
|
|
|
|
console.log('WP DATA: Full releaseData object received:', releaseData);
|
|
|
|
try {
|
|
const settings = await new Promise(resolve => chrome.storage.local.get('wpSettings', data => resolve(data.wpSettings)));
|
|
|
|
if (!settings || !settings.endpoint || !settings.key || !settings.secret) {
|
|
throw new Error('WordPress settings are not configured. Please save your settings first.');
|
|
}
|
|
|
|
console.log('WP SETTINGS: Found endpoint and credentials.');
|
|
|
|
// Safely construct the payload
|
|
const payload = {
|
|
release_id: releaseData.id || 'N/A',
|
|
price: releaseData.price || '0.00',
|
|
comment: releaseData.comment || '',
|
|
media_condition: releaseData.mediaCondition || 'N/A',
|
|
sleeve_condition: releaseData.sleeveCondition || 'N/A',
|
|
apple_id: releaseData.appleId || 'N/A',
|
|
market_data: {
|
|
have: releaseData.have || 0,
|
|
want: releaseData.want || 0,
|
|
num_for_sale: releaseData.num_for_sale || 0,
|
|
last_sold: releaseData.lastSold || 'N/A',
|
|
low_price: releaseData.lowPrice || 'N/A',
|
|
median_price: releaseData.medianPrice || 'N/A',
|
|
high_price: releaseData.highPrice || 'N/A',
|
|
}
|
|
};
|
|
|
|
console.log('WP PAYLOAD: Sending the following data to WordPress:', JSON.stringify(payload, null, 2));
|
|
|
|
const response = await fetch(settings.endpoint, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-WP-API-Key': settings.key,
|
|
'X-WP-API-Secret': settings.secret
|
|
},
|
|
body: JSON.stringify(payload)
|
|
});
|
|
|
|
const responseText = await response.text();
|
|
|
|
if (response.ok) {
|
|
console.log('WP SUCCESS: Server responded with:', responseText);
|
|
alert('Successfully sent to WordPress!');
|
|
} else {
|
|
console.error('WP ERROR: Server responded with an error.', `Status: ${response.status}`, `Response: ${responseText}`);
|
|
throw new Error(`WordPress API Error: ${responseText}`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('WP FATAL: An exception occurred during the WordPress import process.', error);
|
|
alert(`An error occurred: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async function importToWordPress(releaseData) {
|
|
// console.log('Starting WordPress import with data:', releaseData);
|
|
|
|
try {
|
|
const wpSettings = await getWpSettings();
|
|
if (!wpSettings || !wpSettings.endpoint || !wpSettings.key || !wpSettings.secret) {
|
|
return { success: false, message: 'WordPress settings not configured' };
|
|
}
|
|
|
|
// console.log('WordPress settings loaded:', {
|
|
// endpoint: wpSettings.endpoint,
|
|
// hasKey: !!wpSettings.key,
|
|
// hasSecret: !!wpSettings.secret
|
|
// });
|
|
|
|
const postData = {
|
|
title: `${releaseData.artist} - ${releaseData.title}`,
|
|
content: generateWordPressContent(releaseData),
|
|
status: 'draft',
|
|
categories: [1], // Default category
|
|
tags: releaseData.genres ? releaseData.genres.join(', ') : ''
|
|
};
|
|
|
|
// console.log('Prepared post data:', postData);
|
|
|
|
const auth = btoa(`${wpSettings.key}:${wpSettings.secret}`);
|
|
|
|
const response = await fetch(`${wpSettings.endpoint}/wp-json/wp/v2/posts`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Basic ${auth}`
|
|
},
|
|
body: JSON.stringify(postData)
|
|
});
|
|
|
|
// console.log('WordPress API response status:', response.status);
|
|
|
|
if (response.ok) {
|
|
const result = await response.json();
|
|
// console.log('WordPress post created successfully:', result);
|
|
return {
|
|
success: true,
|
|
message: `WordPress post created successfully! Post ID: ${result.id}`,
|
|
postId: result.id,
|
|
editUrl: result.link
|
|
};
|
|
} else {
|
|
const errorText = await response.text();
|
|
// console.error('WordPress API error:', response.status, errorText);
|
|
return {
|
|
success: false,
|
|
message: `WordPress API error: ${response.status} - ${errorText}`
|
|
};
|
|
}
|
|
|
|
} catch (error) {
|
|
// console.error('Error importing to WordPress:', error);
|
|
return {
|
|
success: false,
|
|
message: `Error importing to WordPress: ${error.message}`
|
|
};
|
|
}
|
|
}
|
|
|
|
async function getWpSettings() {
|
|
return new Promise((resolve) => {
|
|
chrome.storage.local.get(['wpSettings'], (result) => {
|
|
// console.log('Retrieved WordPress settings:', result.wpSettings);
|
|
resolve(result.wpSettings || null);
|
|
});
|
|
});
|
|
} |