MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 8: | Line 8: | ||
importScript("MediaWiki:EventForm.js"); | importScript("MediaWiki:EventForm.js"); | ||
} | } | ||
/* From the table? */ | |||
$(document).ready(function () { | |||
// Check if we are on a page, not an edit or special page | |||
if (mw.config.get("wgNamespaceNumber") < 0) return; | |||
var pageTitle = mw.config.get("wgPageName"); | |||
// Fetch categories of the current page | |||
$.get(mw.util.wikiScript("api"), { | |||
action: "query", | |||
prop: "categories", | |||
titles: pageTitle, | |||
format: "json" | |||
}) | |||
.done(function (data) { | |||
var pageId = Object.keys(data.query.pages)[0]; | |||
var categories = data.query.pages[pageId].categories || []; | |||
// Check if the page belongs to "Category:Events" | |||
var isEventPage = categories.some(cat => cat.title === "Category:Events"); | |||
if (!isEventPage) return; // Exit if not an event page | |||
console.log("Detected an event page. Checking for submission..."); | |||
// Fetch page content | |||
$.get(mw.util.wikiScript("api"), { | |||
action: "query", | |||
prop: "revisions", | |||
rvprop: "content", | |||
format: "json", | |||
titles: pageTitle | |||
}) | |||
.done(function (data) { | |||
var pageContent = data.query.pages[Object.keys(data.query.pages)[0]].revisions[0]["*"]; | |||
console.log("Extracted event content:", pageContent); | |||
// Extract event details (adjust based on how your event pages are formatted) | |||
var eventData = { | |||
eventTitle: pageTitle.replace(/_/g, " "), | |||
eventDate: pageContent.match(/\| Date = (.+)/)?.[1] || "Unknown", | |||
eventTime: pageContent.match(/\| Time = (.+)/)?.[1] || "Unknown", | |||
eventLocation: pageContent.match(/\| Location = (.+)/)?.[1] || "Unknown", | |||
eventDescription: pageContent.match(/\| Description = (.+)/)?.[1] || "Unknown", | |||
submittedBy: mw.config.get("wgUserName") | |||
}; | |||
console.log("Parsed Event Data:", eventData); | |||
// Send to Google Sheets | |||
fetch("https://script.google.com/macros/s/YOUR_DEPLOYED_SCRIPT_ID/exec", { | |||
method: "POST", | |||
body: JSON.stringify(eventData), | |||
headers: { "Content-Type": "application/json" } | |||
}) | |||
.then(response => response.text()) | |||
.then(data => { | |||
console.log("Google Sheets Response:", data); | |||
alert("Event successfully logged in Google Calendar."); | |||
}) | |||
.catch(error => { | |||
console.error("Error:", error); | |||
alert("Failed to log event."); | |||
}); | |||
}); | |||
}); | |||
}); | |||
Latest revision as of 17:37, 25 March 2025
/* Any JavaScript here will be loaded for all users on every page load. */
if (mw.config.get("wgPageName") === "Form:EventForm") {
importScript("MediaWiki:EventForm.js");
}
if (mw.config.get("wgPageName").startsWith("Special:FormEdit/EventForm")) {
importScript("MediaWiki:EventForm.js");
}
/* From the table? */
$(document).ready(function () {
// Check if we are on a page, not an edit or special page
if (mw.config.get("wgNamespaceNumber") < 0) return;
var pageTitle = mw.config.get("wgPageName");
// Fetch categories of the current page
$.get(mw.util.wikiScript("api"), {
action: "query",
prop: "categories",
titles: pageTitle,
format: "json"
})
.done(function (data) {
var pageId = Object.keys(data.query.pages)[0];
var categories = data.query.pages[pageId].categories || [];
// Check if the page belongs to "Category:Events"
var isEventPage = categories.some(cat => cat.title === "Category:Events");
if (!isEventPage) return; // Exit if not an event page
console.log("Detected an event page. Checking for submission...");
// Fetch page content
$.get(mw.util.wikiScript("api"), {
action: "query",
prop: "revisions",
rvprop: "content",
format: "json",
titles: pageTitle
})
.done(function (data) {
var pageContent = data.query.pages[Object.keys(data.query.pages)[0]].revisions[0]["*"];
console.log("Extracted event content:", pageContent);
// Extract event details (adjust based on how your event pages are formatted)
var eventData = {
eventTitle: pageTitle.replace(/_/g, " "),
eventDate: pageContent.match(/\| Date = (.+)/)?.[1] || "Unknown",
eventTime: pageContent.match(/\| Time = (.+)/)?.[1] || "Unknown",
eventLocation: pageContent.match(/\| Location = (.+)/)?.[1] || "Unknown",
eventDescription: pageContent.match(/\| Description = (.+)/)?.[1] || "Unknown",
submittedBy: mw.config.get("wgUserName")
};
console.log("Parsed Event Data:", eventData);
// Send to Google Sheets
fetch("https://script.google.com/macros/s/YOUR_DEPLOYED_SCRIPT_ID/exec", {
method: "POST",
body: JSON.stringify(eventData),
headers: { "Content-Type": "application/json" }
})
.then(response => response.text())
.then(data => {
console.log("Google Sheets Response:", data);
alert("Event successfully logged in Google Calendar.");
})
.catch(error => {
console.error("Error:", error);
alert("Failed to log event.");
});
});
});
});