Update htaccess to include rewrite rules for non-existing files and directories
This commit is contained in:
602
apilo.js
602
apilo.js
@@ -1,12 +1,8 @@
|
|||||||
// ==UserScript==
|
// ==UserScript==
|
||||||
// @name A
|
// @name A
|
||||||
// @version 2.2
|
// @version 1
|
||||||
// @grant none
|
// @grant none
|
||||||
// ==/UserScript==
|
// ==/UserScript==
|
||||||
|
|
||||||
(function() {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const currentUrl = window.location.href;
|
const currentUrl = window.location.href;
|
||||||
|
|
||||||
const patterns = [
|
const patterns = [
|
||||||
@@ -17,213 +13,57 @@
|
|||||||
/^https:\/\/projectpro\.apilo\.com\/order\/order\/all\/?$/
|
/^https:\/\/projectpro\.apilo\.com\/order\/order\/all\/?$/
|
||||||
];
|
];
|
||||||
|
|
||||||
const orderDetailUrlPattern = /https:\/\/projectpro\.apilo\.com\/order\/order\/detail\/.*\?static=shipment/;
|
|
||||||
if (orderDetailUrlPattern.test(currentUrl)) {
|
|
||||||
handlePrintButton();
|
|
||||||
}
|
|
||||||
|
|
||||||
function handlePrintButton() {
|
|
||||||
const checkInterval = 250;
|
|
||||||
const maxRetries = 20;
|
|
||||||
let attempts = 0;
|
|
||||||
|
|
||||||
const intervalId = setInterval(() => {
|
|
||||||
const alertElevateElements = document.querySelectorAll('.alert-elevate');
|
|
||||||
if (alertElevateElements.length > 0) {
|
|
||||||
let printButtons = [];
|
|
||||||
alertElevateElements.forEach(alert => {
|
|
||||||
const buttons = Array.from(alert.querySelectorAll('a')).filter(button => button.textContent.trim() === "Drukuj");
|
|
||||||
printButtons = printButtons.concat(buttons);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (printButtons.length > 0) {
|
|
||||||
clearInterval(intervalId);
|
|
||||||
const lastPrintButton = printButtons[printButtons.length - 1];
|
|
||||||
lastPrintButton.click();
|
|
||||||
waitForModalAndClickXprinter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
attempts++;
|
|
||||||
if (attempts > maxRetries) {
|
|
||||||
clearInterval(intervalId);
|
|
||||||
console.error("Nie znaleziono przycisku 'Drukuj' po maksymalnej liczbie prób.");
|
|
||||||
}
|
|
||||||
}, checkInterval);
|
|
||||||
}
|
|
||||||
|
|
||||||
function waitForModalAndClickXprinter() {
|
|
||||||
const modalCheckInterval = 250;
|
|
||||||
const maxModalRetries = 20;
|
|
||||||
let modalAttempts = 0;
|
|
||||||
|
|
||||||
const modalIntervalId = setInterval(() => {
|
|
||||||
const modal = document.querySelector('.modal');
|
|
||||||
if (modal) {
|
|
||||||
const xprinterButton = Array.from(modal.querySelectorAll('a')).find(button => button.textContent.trim() === "Xprinter XP-420B");
|
|
||||||
if (xprinterButton) {
|
|
||||||
clearInterval(modalIntervalId);
|
|
||||||
xprinterButton.click();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
modalAttempts++;
|
|
||||||
if (modalAttempts > maxModalRetries) {
|
|
||||||
clearInterval(modalIntervalId);
|
|
||||||
console.error("Nie znaleziono przycisku 'Xprinter XP-420B' po maksymalnej liczbie prób.");
|
|
||||||
}
|
|
||||||
}, modalCheckInterval);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (patterns.some(pattern => pattern.test(currentUrl))) {
|
if (patterns.some(pattern => pattern.test(currentUrl))) {
|
||||||
waitForElement('.dataTables_scrollBody').then(() => {
|
waitForTableAndSetImage();
|
||||||
waitForTableAndSetImage();
|
attachTableReloadListener();
|
||||||
attachTableReloadListener();
|
|
||||||
}).catch(error => {
|
|
||||||
console.error('Nie udało się znaleźć elementu .dataTables_scrollBody:', error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentUrl2 = window.location.href;
|
|
||||||
const patternWarehouse = /^https:\/\/projectpro\.apilo\.com\/warehouse\/shipment\/new-for-order\/.+/;
|
|
||||||
|
|
||||||
if (patternWarehouse.test(currentUrl2)) {
|
|
||||||
waitForElement('.kt-portlet__body').then(portletBody => {
|
|
||||||
const buttonContainer = createButtonContainer();
|
|
||||||
portletBody.parentNode.insertBefore(buttonContainer, portletBody);
|
|
||||||
|
|
||||||
const buttons = [
|
|
||||||
createButton('Apaczka D2D Inpost', '#7039df', 'ApaczkaD2D'),
|
|
||||||
createButton('Apaczka P2P Inpost', '#7039df', 'ApaczkaP2P'),
|
|
||||||
createButton('Furgonetka P2D Inpost', '#ff7800', 'P2D.inpost', 'RZE14N||RZE14N'),
|
|
||||||
createButton('Furgonetka D2P Inpost', '#ff7800', 'D2P.inpost'),
|
|
||||||
createButton('Furgonetka D2D Inpost', '#ff7800', 'D2D.inpostkurier'),
|
|
||||||
createButton('Furgonetka P2P Inpost', '#ff7800', 'P2P.inpost', 'RZE14N||RZE14N')
|
|
||||||
];
|
|
||||||
|
|
||||||
buttons.forEach(button => buttonContainer.appendChild(button));
|
|
||||||
}).catch(error => {
|
|
||||||
console.error('Nie udało się znaleźć elementu .kt-portlet__body:', error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja oczekująca na pojawienie się elementu w DOM.
|
|
||||||
* @param {string|Element} selector - Selektor CSS lub bezpośredni element.
|
|
||||||
* @param {number} interval - Czas oczekiwania między próbami w ms.
|
|
||||||
* @param {number} retries - Maksymalna liczba prób.
|
|
||||||
* @returns {Promise<Element>} - Obietnica z elementem lub odrzucenie.
|
|
||||||
*/
|
|
||||||
function waitForElement(selector, interval = 500, retries = 10) {
|
|
||||||
return retryUntilSuccess(() => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let element;
|
|
||||||
if (typeof selector === 'string') {
|
|
||||||
element = document.querySelector(selector);
|
|
||||||
} else if (selector instanceof Element) {
|
|
||||||
element = selector;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (element) {
|
|
||||||
resolve(element);
|
|
||||||
} else {
|
|
||||||
reject(`Element nie znaleziony: ${selector}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, interval, retries);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja retryUntilSuccess próbuje wykonać funkcję do skutku z określonymi interwałami i limitami prób.
|
|
||||||
* @param {Function} fn - Funkcja zwracająca obietnicę.
|
|
||||||
* @param {number} interval - Czas oczekiwania między próbami w ms.
|
|
||||||
* @param {number} retries - Maksymalna liczba prób.
|
|
||||||
* @returns {Promise} - Obietnica rozwiązana lub odrzucona.
|
|
||||||
*/
|
|
||||||
function retryUntilSuccess(fn, interval = 500, retries = 10) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const attempt = async () => {
|
|
||||||
try {
|
|
||||||
console.log(`Próba wykonania funkcji. Pozostało prób: ${retries}`);
|
|
||||||
const result = await fn();
|
|
||||||
resolve(result);
|
|
||||||
} catch (err) {
|
|
||||||
if (retries === 0) {
|
|
||||||
console.error('Maksymalna liczba prób osiągnięta. Funkcja nie powiodła się:', err);
|
|
||||||
reject(err);
|
|
||||||
} else {
|
|
||||||
console.warn(`Nie znaleziono elementu. Próba ponowna za ${interval} ms. Pozostało prób: ${retries}`);
|
|
||||||
setTimeout(() => {
|
|
||||||
retries--;
|
|
||||||
attempt();
|
|
||||||
}, interval);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
attempt();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja oczekująca na pojawienie się tabeli i ustawiająca obrazki.
|
|
||||||
*/
|
|
||||||
function waitForTableAndSetImage() {
|
function waitForTableAndSetImage() {
|
||||||
waitForElement('.dataTables_scrollBody').then(() => {
|
const intervalId = setInterval(() => {
|
||||||
const dataTables_tbody = document.querySelector('.dataTables_scrollBody tbody');
|
let dataTables_scrollBody = document.getElementsByClassName('dataTables_scrollBody');
|
||||||
const rows = dataTables_tbody ? dataTables_tbody.getElementsByTagName('tr') : [];
|
if (dataTables_scrollBody.length > 0) {
|
||||||
|
let dataTables_tbody = dataTables_scrollBody[0].getElementsByTagName('tbody')[0];
|
||||||
|
let rows = dataTables_tbody.getElementsByTagName('tr');
|
||||||
|
|
||||||
if (rows.length > 0) {
|
if (rows.length > 0) {
|
||||||
setImageToProduct();
|
clearInterval(intervalId); // Zatrzymanie dalszego wykonywania setInterval
|
||||||
} else {
|
setImageToProduct();
|
||||||
console.warn('Brak wierszy w tabeli. Czekam na pojawienie się danych...');
|
|
||||||
// Można dodać dodatkowe logiki tutaj, jeśli potrzebne
|
|
||||||
}
|
|
||||||
}).catch(error => {
|
|
||||||
console.error('Nie udało się znaleźć tabeli .dataTables_scrollBody:', error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja dołączająca nasłuchiwacz na zmiany w tabeli.
|
|
||||||
*/
|
|
||||||
function attachTableReloadListener() {
|
|
||||||
waitForElement('.dataTables_scrollBody table').then(table => {
|
|
||||||
const observer = new MutationObserver((mutationsList, observer) => {
|
|
||||||
for (const mutation of mutationsList) {
|
|
||||||
if (mutation.type === 'childList') {
|
|
||||||
console.log('Zawartość tabeli została zmieniona.');
|
|
||||||
waitForTableAndSetImage();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const tbody = table.querySelector('tbody');
|
|
||||||
if (tbody) {
|
|
||||||
observer.observe(tbody, { childList: true, subtree: true });
|
|
||||||
console.log('Obserwator zmian w tabeli został ustawiony.');
|
|
||||||
} else {
|
|
||||||
console.warn('Nie znaleziono tbody w tabeli.');
|
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}, 100); // Sprawdza co 100ms, można dostosować czas
|
||||||
console.error('Nie udało się znaleźć tabeli do dołączenia obserwatora:', error);
|
}
|
||||||
});
|
|
||||||
|
function attachTableReloadListener() {
|
||||||
|
const table = document.querySelector('.dataTables_scrollBody table');
|
||||||
|
if (table) {
|
||||||
|
// Tworzymy obserwatora zmian w DOM
|
||||||
|
const observer = new MutationObserver((mutationsList, observer) => {
|
||||||
|
for (const mutation of mutationsList) {
|
||||||
|
if (mutation.type === 'childList') {
|
||||||
|
// Wykonaj kod po zmianie w tabeli (dodanie/usunięcie wierszy)
|
||||||
|
console.log('Table content changed');
|
||||||
|
waitForTableAndSetImage();
|
||||||
|
break; // Zatrzymujemy iterację po pierwszej znalezionej zmianie
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Konfiguracja obserwatora do nasłuchiwania zmian w zawartości tabeli
|
||||||
|
observer.observe(table.querySelector('tbody'), { childList: true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca obrazki do produktów.
|
|
||||||
* @param {string} img - URL obrazka (opcjonalny).
|
|
||||||
*/
|
|
||||||
function setImageToProduct(img = '') {
|
function setImageToProduct(img = '') {
|
||||||
waitForElement('.dataTables_scrollBody').then(dataTablesScrollBody => {
|
let dataTables_scrollBody = document.getElementsByClassName('dataTables_scrollBody');
|
||||||
const dataTables_tbody = dataTablesScrollBody.querySelector('tbody');
|
if (dataTables_scrollBody.length > 0) {
|
||||||
const rows = dataTables_tbody ? dataTables_tbody.getElementsByTagName('tr') : [];
|
let dataTables_tbody = dataTables_scrollBody[0].getElementsByTagName('tbody')[0];
|
||||||
|
let rows = dataTables_tbody.getElementsByTagName('tr');
|
||||||
|
|
||||||
for (let i = 0; i < rows.length; i++) {
|
for (let i = 0; i < rows.length; i++) {
|
||||||
const cells = rows[i].getElementsByTagName('td');
|
let cells = rows[i].getElementsByTagName('td');
|
||||||
|
|
||||||
if (cells.length > 1) {
|
if (cells.length > 1) {
|
||||||
const secondCellText = cells[1].textContent.trim();
|
let secondCellText = cells[1].textContent.trim();
|
||||||
let domain;
|
let domain;
|
||||||
|
|
||||||
if (secondCellText.includes('marianek.pl')) {
|
if (secondCellText.includes('marianek.pl')) {
|
||||||
@@ -235,38 +75,37 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cells.length >= 5) {
|
if (cells.length >= 5) {
|
||||||
const fifthCell = cells[4];
|
let fifthCell = cells[4];
|
||||||
const divsInFifthCell = fifthCell.children;
|
let divsInFifthCell = fifthCell.children;
|
||||||
|
|
||||||
for (let j = 0; j < divsInFifthCell.length; j++) {
|
for (let i = 0; i < divsInFifthCell.length; i++) {
|
||||||
const currentDiv = divsInFifthCell[j];
|
let currentDiv = divsInFifthCell[i];
|
||||||
const imgDiv = currentDiv.getElementsByTagName('div')[0];
|
let imgDiv = currentDiv.getElementsByTagName('div')[0];
|
||||||
const dataDiv = currentDiv.getElementsByTagName('div')[1];
|
let dataDiv = currentDiv.getElementsByTagName('div')[1];
|
||||||
|
|
||||||
if (dataDiv) {
|
if (dataDiv) {
|
||||||
const skuMatch = dataDiv.innerHTML.match(/SKU:\s*([A-Za-z0-9-]+)/);
|
let skuText = dataDiv.innerHTML.match(/SKU:\s*([A-Za-z0-9-]+)/);
|
||||||
|
|
||||||
if (skuMatch && skuMatch[1]) {
|
if (skuText && skuText[1]) {
|
||||||
const sku = skuMatch[1];
|
getProductData(skuText[1], domain).then(data => {
|
||||||
getProductData(sku, domain).then(data => {
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
console.log('Produkt nie znaleziony:', sku);
|
console.log('Product not found:', skuText[1]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Produkt znaleziony:', sku);
|
console.log('Product found:', skuText[1]);
|
||||||
imgDiv.innerHTML = '';
|
imgDiv.innerHTML = '';
|
||||||
const imgElement = makeImg(data);
|
const imgElement = makeImg(data);
|
||||||
imgDiv.appendChild(imgElement);
|
imgDiv.appendChild(imgElement);
|
||||||
|
|
||||||
// Dodanie eventu na hover
|
// Dodanie eventu na hover
|
||||||
imgElement.addEventListener('mouseover', () => {
|
imgElement.addEventListener('mouseover', function() {
|
||||||
showLargeImage(data, imgElement);
|
showLargeImage(data, imgElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
imgElement.addEventListener('mouseout', hideLargeImage);
|
imgElement.addEventListener('mouseout', function() {
|
||||||
}).catch(error => {
|
hideLargeImage();
|
||||||
console.error('Błąd podczas pobierania danych produktu:', error);
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,16 +113,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}
|
||||||
console.error('Nie udało się ustawić obrazków do produktów:', error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja tworząca element obrazka.
|
|
||||||
* @param {string} src - URL obrazka.
|
|
||||||
* @returns {HTMLImageElement} - Element img.
|
|
||||||
*/
|
|
||||||
function makeImg(src = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png') {
|
function makeImg(src = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png') {
|
||||||
const img = document.createElement('img');
|
const img = document.createElement('img');
|
||||||
img.src = src;
|
img.src = src;
|
||||||
@@ -291,22 +123,11 @@
|
|||||||
img.className = 'img-fluid center-block';
|
img.className = 'img-fluid center-block';
|
||||||
img.style.maxWidth = '48px';
|
img.style.maxWidth = '48px';
|
||||||
img.style.maxHeight = '48px';
|
img.style.maxHeight = '48px';
|
||||||
img.style.cursor = 'pointer';
|
|
||||||
|
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja pokazująca powiększony obrazek przy najechaniu kursorem.
|
|
||||||
* @param {string} src - URL dużego obrazka.
|
|
||||||
* @param {HTMLImageElement} imgElement - Element img, na którym najechano.
|
|
||||||
*/
|
|
||||||
function showLargeImage(src, imgElement) {
|
function showLargeImage(src, imgElement) {
|
||||||
const existingLargeImg = document.getElementById('largeImagePreview');
|
|
||||||
if (existingLargeImg) {
|
|
||||||
existingLargeImg.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
const largeImg = document.createElement('img');
|
const largeImg = document.createElement('img');
|
||||||
largeImg.src = src;
|
largeImg.src = src;
|
||||||
largeImg.style.position = 'absolute';
|
largeImg.style.position = 'absolute';
|
||||||
@@ -315,16 +136,13 @@
|
|||||||
largeImg.style.border = '1px solid #ccc';
|
largeImg.style.border = '1px solid #ccc';
|
||||||
largeImg.style.background = '#fff';
|
largeImg.style.background = '#fff';
|
||||||
largeImg.style.zIndex = '1000';
|
largeImg.style.zIndex = '1000';
|
||||||
largeImg.style.top = (imgElement.getBoundingClientRect().top + window.scrollY) + 'px';
|
largeImg.style.top = imgElement.getBoundingClientRect().top + window.scrollY + 'px';
|
||||||
largeImg.style.left = (imgElement.getBoundingClientRect().left + imgElement.offsetWidth + 10) + 'px';
|
largeImg.style.left = imgElement.getBoundingClientRect().left + imgElement.offsetWidth + 10 + 'px';
|
||||||
largeImg.id = 'largeImagePreview';
|
largeImg.id = 'largeImagePreview';
|
||||||
|
|
||||||
document.body.appendChild(largeImg);
|
document.body.appendChild(largeImg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ukrywająca powiększony obrazek.
|
|
||||||
*/
|
|
||||||
function hideLargeImage() {
|
function hideLargeImage() {
|
||||||
const largeImg = document.getElementById('largeImagePreview');
|
const largeImg = document.getElementById('largeImagePreview');
|
||||||
if (largeImg) {
|
if (largeImg) {
|
||||||
@@ -332,12 +150,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja pobierająca dane produktu na podstawie SKU i domeny.
|
|
||||||
* @param {string} sku - SKU produktu.
|
|
||||||
* @param {string} domain - Domena (marianek.pl lub pomysloweprezenty.pl).
|
|
||||||
* @returns {Promise<string|null>} - URL obrazka lub null w przypadku błędu.
|
|
||||||
*/
|
|
||||||
async function getProductData(sku, domain) {
|
async function getProductData(sku, domain) {
|
||||||
let url;
|
let url;
|
||||||
if (domain === 'marianek.pl') {
|
if (domain === 'marianek.pl') {
|
||||||
@@ -345,46 +157,49 @@
|
|||||||
} else if (domain === 'pomysloweprezenty.pl') {
|
} else if (domain === 'pomysloweprezenty.pl') {
|
||||||
url = `https://pomysloweprezenty.pl/api/v1/product.php?sku=${sku}`;
|
url = `https://pomysloweprezenty.pl/api/v1/product.php?sku=${sku}`;
|
||||||
} else {
|
} else {
|
||||||
console.error('Niewspierana domena:', domain);
|
console.error('Unsupported domain:', domain);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Błąd HTTP! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json(); console.log(data);
|
||||||
console.log('Dane produktu:', data);
|
return data.img;
|
||||||
return data.img || null;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Błąd podczas pobierania danych produktu:', url, error);
|
console.error('Error fetching product data: ' + url, error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
const currentUrl2 = window.location.href;
|
||||||
* Funkcja tworząca kontener na przyciski.
|
const pattern = /^https:\/\/projectpro\.apilo\.com\/warehouse\/shipment\/new-for-order\/.+/;
|
||||||
* @returns {HTMLDivElement} - Kontener z przyciskami.
|
|
||||||
*/
|
if (pattern.test(currentUrl2)) {
|
||||||
function createButtonContainer() {
|
const portletBody = document.querySelector('.kt-portlet__body');
|
||||||
const buttonContainer = document.createElement('div');
|
if (portletBody) {
|
||||||
buttonContainer.className = 'custom-button-container';
|
const buttonContainer = document.createElement('div');
|
||||||
buttonContainer.style.display = 'flex';
|
buttonContainer.className = 'custom-button-container';
|
||||||
buttonContainer.style.gap = '10px';
|
buttonContainer.style.display = 'flex';
|
||||||
buttonContainer.style.marginBottom = '15px';
|
buttonContainer.style.gap = '10px';
|
||||||
return buttonContainer;
|
buttonContainer.style.marginBottom = '15px';
|
||||||
|
portletBody.parentNode.insertBefore(buttonContainer, portletBody);
|
||||||
|
|
||||||
|
const buttonP2D = createButton('Inpost P2D', '#007bff', 'P2D.inpost', 'RZE14N||RZE14N');
|
||||||
|
const buttonD2D = createButton('Inpost D2D', '#ff7800', 'D2D.inpostkurier');
|
||||||
|
const buttonD2P = createButton('Inpost D2P', '#28a745', 'D2P.inpost');
|
||||||
|
const buttonP2P = createButton('Inpost P2P', '#ffc107', 'P2P.inpost', 'RZE14N||RZE14N');
|
||||||
|
|
||||||
|
buttonContainer.appendChild(buttonP2D);
|
||||||
|
buttonContainer.appendChild(buttonD2D);
|
||||||
|
buttonContainer.appendChild(buttonD2P);
|
||||||
|
buttonContainer.appendChild(buttonP2P);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja tworząca przycisk.
|
|
||||||
* @param {string} text - Tekst na przycisku.
|
|
||||||
* @param {string} backgroundColor - Kolor tła przycisku.
|
|
||||||
* @param {string} method - Metoda wysyłki.
|
|
||||||
* @param {string|null} dropoffPoint - Punkt odbioru (opcjonalny).
|
|
||||||
* @returns {HTMLButtonElement} - Element przycisku.
|
|
||||||
*/
|
|
||||||
function createButton(text, backgroundColor, method, dropoffPoint = null) {
|
function createButton(text, backgroundColor, method, dropoffPoint = null) {
|
||||||
const button = document.createElement('button');
|
const button = document.createElement('button');
|
||||||
button.textContent = text;
|
button.textContent = text;
|
||||||
@@ -397,200 +212,125 @@
|
|||||||
button.style.color = '#FFF';
|
button.style.color = '#FFF';
|
||||||
button.style.border = 'none';
|
button.style.border = 'none';
|
||||||
button.style.cursor = 'pointer';
|
button.style.cursor = 'pointer';
|
||||||
button.style.borderRadius = '4px';
|
|
||||||
button.style.fontSize = '14px';
|
|
||||||
|
|
||||||
button.addEventListener('click', () => handleShipment(method, dropoffPoint));
|
button.addEventListener('click', () => handleShipment(method, dropoffPoint));
|
||||||
|
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja obsługująca kliknięcie przycisku wysyłki.
|
|
||||||
* @param {string} method - Metoda wysyłki.
|
|
||||||
* @param {string|null} dropoffPoint - Punkt odbioru (opcjonalny).
|
|
||||||
*/
|
|
||||||
async function handleShipment(method, dropoffPoint = null) {
|
async function handleShipment(method, dropoffPoint = null) {
|
||||||
try {
|
try {
|
||||||
if (method.startsWith('Apaczka')) {
|
await selectPackageType();
|
||||||
console.log('Ustawianie wysyłki Apaczka...');
|
await setShipmentMethod(method);
|
||||||
await setCarrierAccount();
|
if (dropoffPoint) {
|
||||||
await setShipmentMethodApaczka(method);
|
await setDropoffPoint(dropoffPoint);
|
||||||
|
|
||||||
if (method === 'ApaczkaP2P') {
|
|
||||||
await setDropoffPoint('RZE14N||RZE14N');
|
|
||||||
}
|
|
||||||
|
|
||||||
await setPreferencesContentApaczka();
|
|
||||||
await setParcelWeight('1');
|
|
||||||
await submitShipment();
|
|
||||||
} else {
|
|
||||||
console.log(`Ustawianie wysyłki metodą: ${method}`);
|
|
||||||
await selectPackageType();
|
|
||||||
await setShipmentMethod(method);
|
|
||||||
if (dropoffPoint) {
|
|
||||||
await setDropoffPoint(dropoffPoint);
|
|
||||||
}
|
|
||||||
await setParcelWeight('1');
|
|
||||||
await submitShipment();
|
|
||||||
}
|
}
|
||||||
console.log('Wysyłka została pomyślnie ustawiona.');
|
await setParcelWeight('1');
|
||||||
|
await submitShipment();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Wystąpił błąd podczas ustawiania wysyłki:', error);
|
console.error('Wystąpił błąd: ', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function retryUntilSuccess(fn, interval = 500, retries = 10) {
|
||||||
* Funkcja ustawiająca konto przewoźnika.
|
return new Promise((resolve, reject) => {
|
||||||
* @returns {Promise<void>}
|
const attempt = async () => {
|
||||||
*/
|
try {
|
||||||
function setCarrierAccount() {
|
const result = await fn();
|
||||||
return retryUntilSuccess(() => {
|
resolve(result);
|
||||||
return new Promise((resolve, reject) => {
|
} catch (err) {
|
||||||
const carrierAccountElement = document.getElementById('warehousebundle_shipment_carrierAccount');
|
if (retries === 0) {
|
||||||
if (carrierAccountElement) {
|
reject(err);
|
||||||
carrierAccountElement.value = '17';
|
} else {
|
||||||
const event = new Event('change', { bubbles: true });
|
setTimeout(() => {
|
||||||
carrierAccountElement.dispatchEvent(event);
|
retries--;
|
||||||
console.log('Konto przewoźnika ustawione na 17.');
|
attempt();
|
||||||
resolve();
|
}, interval);
|
||||||
} else {
|
}
|
||||||
reject('Nie znaleziono elementu warehousebundle_shipment_carrierAccount');
|
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
attempt();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca metodę wysyłki na podstawie typu Apaczki.
|
|
||||||
* Upewnia się, że select nie jest pusty przed ustawieniem wartości.
|
|
||||||
* @param {string} method - Typ Apaczki ('ApaczkaD2D' lub 'ApaczkaP2P').
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function setShipmentMethodApaczka(method) {
|
|
||||||
return retryUntilSuccess(() => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const methodElement = document.getElementById('warehousebundle_shipment_method'); console.log( methodElement );
|
|
||||||
if (methodElement) {
|
|
||||||
if (methodElement.options.length > 0) {
|
|
||||||
methodElement.value = (method === 'ApaczkaP2P') ? '41' : '42';
|
|
||||||
const event = new Event('change', { bubbles: true });
|
|
||||||
methodElement.dispatchEvent(event);
|
|
||||||
console.log(`Metoda wysyłki ustawiona na ${(method === 'ApaczkaP2P') ? '49 (ApaczkaP2P)' : '42 (ApaczkaD2D)'}.`);
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject('Select #warehousebundle_shipment_method jest pusty.');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
reject('Nie znaleziono elementu warehousebundle_shipment_method');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 500, 20).catch(error => {
|
|
||||||
console.error('Nie udało się ustawić metody wysyłki:', error);
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca preferencje zawartości na "prezent" dla Apaczki.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function setPreferencesContentApaczka() {
|
|
||||||
return retryUntilSuccess(() => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const contentElement = document.getElementById('warehousebundle_shipment_preferences_content');
|
|
||||||
if (contentElement) {
|
|
||||||
contentElement.value = 'prezent';
|
|
||||||
const event = new Event('input', { bubbles: true });
|
|
||||||
contentElement.dispatchEvent(event);
|
|
||||||
console.log('Preferencje zawartości ustawione na "prezent".');
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject('Nie znaleziono elementu warehousebundle_shipment_preferences_content');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca typ paczki dla innych metod niż Apaczka.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function selectPackageType() {
|
function selectPackageType() {
|
||||||
return waitForElement('#warehousebundle_shipment_packageType').then(selectElement => {
|
return retryUntilSuccess(() => {
|
||||||
selectElement.value = 'package';
|
return new Promise((resolve, reject) => {
|
||||||
const event = new Event('change', { bubbles: true });
|
const selectElement = document.getElementById('warehousebundle_shipment_packageType');
|
||||||
selectElement.dispatchEvent(event);
|
if (selectElement) {
|
||||||
console.log('Typ paczki ustawiony na package.');
|
selectElement.value = 'package';
|
||||||
}).catch(error => {
|
const event = document.createEvent('HTMLEvents');
|
||||||
console.error('Nie udało się ustawić typu paczki:', error);
|
event.initEvent('change', true, false);
|
||||||
throw error;
|
selectElement.dispatchEvent(event);
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
reject('Nie znaleziono elementu packageType');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca metodę wysyłki.
|
|
||||||
* @param {string} method - Metoda wysyłki do ustawienia.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function setShipmentMethod(method) {
|
function setShipmentMethod(method) {
|
||||||
return waitForElement('#warehousebundle_shipment_method').then(methodElement => {
|
return retryUntilSuccess(() => {
|
||||||
methodElement.value = method;
|
return new Promise((resolve, reject) => {
|
||||||
const event = new Event('change', { bubbles: true });
|
const methodElement = document.getElementById('warehousebundle_shipment_method');
|
||||||
methodElement.dispatchEvent(event);
|
if (methodElement) {
|
||||||
console.log(`Metoda wysyłki ustawiona na ${method}.`);
|
methodElement.value = method;
|
||||||
}).catch(error => {
|
const methodEvent = document.createEvent('HTMLEvents');
|
||||||
console.error('Nie udało się ustawić metody wysyłki:', error);
|
methodEvent.initEvent('change', true, false);
|
||||||
throw error;
|
methodElement.dispatchEvent(methodEvent);
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
reject('Nie znaleziono elementu shipment_method');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca punkt odbioru.
|
|
||||||
* @param {string} dropoffPoint - Punkt odbioru do ustawienia.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function setDropoffPoint(dropoffPoint) {
|
function setDropoffPoint(dropoffPoint) {
|
||||||
return waitForElement('#warehousebundle_shipment_preferences_dropoffPoint').then(dropoffPointElement => {
|
return retryUntilSuccess(() => {
|
||||||
dropoffPointElement.value = dropoffPoint;
|
return new Promise((resolve, reject) => {
|
||||||
const event = new Event('change', { bubbles: true });
|
const dropoffPointElement = document.getElementById('warehousebundle_shipment_preferences_dropoffPoint');
|
||||||
dropoffPointElement.dispatchEvent(event);
|
if (dropoffPointElement) {
|
||||||
console.log(`Punkt odbioru ustawiony na ${dropoffPoint}.`);
|
dropoffPointElement.value = dropoffPoint;
|
||||||
}).catch(error => {
|
const dropoffPointEvent = document.createEvent('HTMLEvents');
|
||||||
console.error('Nie udało się ustawić punktu odbioru:', error);
|
dropoffPointEvent.initEvent('change', true, false);
|
||||||
throw error;
|
dropoffPointElement.dispatchEvent(dropoffPointEvent);
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
reject('Nie znaleziono elementu dropoffPoint');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja ustawiająca wagę paczki.
|
|
||||||
* @param {string} weight - Waga paczki do ustawienia.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function setParcelWeight(weight) {
|
function setParcelWeight(weight) {
|
||||||
return waitForElement('#warehousebundle_shipment_shipmentParcels_0_weight').then(weightElement => {
|
return retryUntilSuccess(() => {
|
||||||
weightElement.value = weight;
|
return new Promise((resolve, reject) => {
|
||||||
const event = new Event('change', { bubbles: true });
|
const weightElement = document.getElementById('warehousebundle_shipment_shipmentParcels_0_weight');
|
||||||
weightElement.dispatchEvent(event);
|
if (weightElement) {
|
||||||
console.log(`Waga paczki ustawiona na ${weight}.`);
|
weightElement.value = weight;
|
||||||
}).catch(error => {
|
const weightEvent = document.createEvent('HTMLEvents');
|
||||||
console.error('Nie udało się ustawić wagi paczki:', error);
|
weightEvent.initEvent('change', true, false);
|
||||||
throw error;
|
weightElement.dispatchEvent(weightEvent);
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
reject('Nie znaleziono elementu shipmentParcels_0_weight');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Funkcja wysyłająca formularz wysyłki.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
function submitShipment() {
|
function submitShipment() {
|
||||||
return waitForElement('#warehousebundle_shipment_buttons_submit').then(submitButton => {
|
return retryUntilSuccess(() => {
|
||||||
submitButton.click();
|
return new Promise((resolve, reject) => {
|
||||||
console.log('Przycisk submit został kliknięty.');
|
const submitButton = document.getElementById('warehousebundle_shipment_buttons_submit');
|
||||||
}).catch(error => {
|
if (submitButton) {
|
||||||
console.error('Nie udało się znaleźć przycisku submit:', error);
|
submitButton.click();
|
||||||
throw error;
|
resolve();
|
||||||
|
} else {
|
||||||
|
reject('Nie znaleziono przycisku submit');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
})();
|
|
||||||
Reference in New Issue
Block a user