משתמש:מוטי בוט/מחיקת עריכות אבודות.js
מראה
לתשומת ליבך: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.
- פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
- גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
- אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
$(document).ready(function () {
const dbName = "hamichlol_main_editRecovery";
const objectStoreName = "unsaved-page-data";
let db = null;
/**
* @ignore
* @return {jQuery.Promise} Promise which resolves on success
*/
function openDatabaseLocal() {
return new Promise((resolve, reject) => {
const schemaNumber = 3;
const openRequest = window.indexedDB.open(dbName, schemaNumber);
openRequest.addEventListener("success", (event) => {
db = event.target.result;
db.onerror = (event) => {
console.error(`Database error: `, event.target.error);
};
resolve();
});
openRequest.addEventListener("error", (event) => {
reject("EditRecovery error: " + event.target.error);
});
});
}
/**
* Delete data relating to a specific page
*
* @ignore
* @param {string} pageName The current page name (with underscores)
* @param {string|null} section The section ID, or null if the whole page is being edited
* @return {jQuery.Promise} Promise which resolves on success, or rejects with an error message.
*/
function deleteData(pageName, section) {
return new Promise((resolve, reject) => {
if (!db) {
reject("DB not opened");
}
const transaction = db.transaction(objectStoreName, "readwrite");
const objectStore = transaction.objectStore(objectStoreName);
const request = objectStore.delete([pageName, section || ""]);
request.addEventListener("success", resolve);
request.addEventListener("error", () => {
reject("Error opening cursor");
});
});
}
function deleteAllData() {
return new Promise((resolve, reject) => {
if (!db) {
reject("DB not opened");
}
const transaction = db.transaction(objectStoreName, "readwrite");
const objectStore = transaction.objectStore(objectStoreName);
const request = objectStore.clear();
request.addEventListener("success", () => {
db.close();
resolve();
});
request.addEventListener("error", reject);
});
}
/**
* Close database
*
* @ignore
*/
function closeDatabase() {
if (db) {
db.close();
}
}
function extractPageInfo(element) {
const fullText = $(element).text();
const pageInfo = {};
const parenthesisIndex = fullText.lastIndexOf("(");
let text = fullText.substring(0, parenthesisIndex).trim();
const parts = text.split(/\s*–\s*/);
pageInfo.name = parts[0].trim().replace(/\s/g, "_");
if (parts.length > 1) {
pageInfo.section = parts[1];
} else {
pageInfo.section = "";
}
return pageInfo;
}
function addElements() {
const rootElement = $(".mw-EditRecovery-special");
rootElement.append(
$("<a href='#'>מחיקת כל הנתונים</a>").click(() => {
deleteAllData().then(() => {
console.log("Successfully deleted all entries");
mw.notify("כל הנתונים נמחקו בהצלחה", { type: "success" });
rootElement.find("ol").empty();
rootElement.find("p").text("אין לך שינויים שלא נשמרו.");
});
})
);
const elements = rootElement.find("li");
elements.each(function () {
const { name, section } = extractPageInfo(this);
const linkClick = $(
`<a href='#' data-name=${encodeURIComponent(
name
)} data-section=${section}>מחיקה</a>`
).click(function () {
const name = decodeURIComponent($(this).data("name"));
deleteData(name, section)
.then(() => {
$(this).closest("li").remove(); // Remove the list item on successful deletion
console.log(`Successfully deleted entry for:`, name, section);
mw.notify(
`הרשומה ${name}${section ? ", " + section : ""} נמחקה בהצלחה`,
{ type: "success" }
);
})
.catch((error) => {
console.error(`Failed to delete entry:`, error);
mw.notify(`שגיאה במחיקת הרשומה ${name}: ${error}`, {
type: "error",
});
});
});
$(this).append(" (").append(linkClick).append(") ");
});
}
if (
mw.config.get("wgCanonicalNamespace") === "Special" &&
mw.config.get("wgCanonicalSpecialPageName") === "EditRecovery"
) {
openDatabaseLocal().then(() => {
addElements();
});
}
});