বিষয়বস্তুতে চলুন

ব্যবহারকারী:খাত্তাব হাসান/test.js

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন।
  • অপেরা: Ctrl-F5 টিপুন।
// ==UserScript==
// @name         Wikipedia User Script
// @namespace    wikipedia-userscript
// @version      1.1-beta
// @description  Add a button to the Wikipedia sidebar to make edits to the talk page.
// @author       Kapudan Pasha
// @match        https://en.wikipedia.org/*
// @grant        none
// ==/UserScript==
// <nowiki>

(function () {
  'use strict';

  // Function to create the form
  function createForm() {
    const form = document.createElement('form');
    form.innerHTML = `
      <label for="options">Select an option:</label>
      <select id="options">
        <option value="option_a">Option A</option>
        <option value="option_b">Option B</option>
        <option value="option_c">Option C</option>
      </select>
      <button type="button" id="cancelButton">Cancel</button>
      <button type="button" id="submitButton">Submit</button>
    `;

    // Add event listeners to buttons
    form.querySelector('#cancelButton').addEventListener('click', closeForm);
    form.querySelector('#submitButton').addEventListener('click', submitForm);

    return form;
  }

  // Function to open the form
  function openForm() {
    const form = createForm();
    const sidebar = document.querySelector('#mw-navigation');
    sidebar.appendChild(form);
  }

  // Function to close the form
  function closeForm() {
    const form = document.querySelector('form');
    form.remove();
  }

  // Function to submit the form and make an edit
  function submitForm() {
    const selectedOption = document.querySelector('#options').value;
    const talkPageContent = `{{d|${selectedOption}}}`;

    // You would need to implement the API request to edit the talk page here
    // Example API request:
    fetch('https://en.wikipedia.org/w/api.php', {
      method: 'POST',
      body: new URLSearchParams({
        action: 'edit',
        title: 'Talk:Page_Title',
        section: 'new',
        text: talkPageContent,
        summary: 'Added option to talk page',
        token: mw.user.tokens.get('csrfToken'),
      }),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    })
      .then(response => response.json())
      .then(data => {
        if (data.edit && data.edit.result === 'Success') {
          // Edit successful, add {{e}} to the page's top if needed
          // Implement {{e}} addition here
          closeForm();
        } else {
          alert('Error: Unable to make the edit.');
        }
      });
  }

  // Add a button to the sidebar
  const addButton = document.createElement('button');
  addButton.textContent = 'Edit Talk Page';
  addButton.addEventListener('click', openForm);
  const sidebar = document.querySelector('#mw-navigation');
  sidebar.appendChild(addButton);
})();

// </nowiki>