User:01tonythomas/common.js

$(document).ready(function {  // Select all section headings  $('.mw-headline').each(function  { var sectionHeading = $(this);

// Find the existing edit link var editLink = sectionHeading.siblings('.mw-editsection');

// Check if the immediate parent is an h2 or mw-heading2 var immediateParent = sectionHeading.parent; if (immediateParent.is('h2') || immediateParent.is('.mw-heading2')) {

// Create the "summarize" link as mw-summarysection var summarizeLink = $(' ') .addClass('mw-summarysection') .addClass('mw-editsection') .css('margin-left', '0.5em') // Set left margin to 0.5em .append(         $(' ')            .addClass('mw-editsection-bracket')            .text('[')        ) .append(         $('')            .attr('href', '#')            .text('summarize')            .click(function (e) { e.preventDefault; summarizeSection(sectionHeading); })       )        .append(          $(' ')            .addClass('mw-editsection-bracket')            .text(']')        );

// Insert the "summarize" link after the edit link editLink.after(summarizeLink); } });

function fetchSummaryFromOpenAPI(openAPIKey, sectionText, callback) { const fixedPromptForChatGPT = "Summarize the following section: ";

$.ajax({     "crossDomain": true,      "url": "https://api.openai.com/v1/chat/completions",      "method": "POST",      "headers": {        "Content-Type": "application/json",        "Authorization": "Bearer " + openAPIKey,      },      "data": JSON.stringify({ "model": "gpt-3.5-turbo", "messages": [ {           "role": "user", "content": fixedPromptForChatGPT + sectionText, }       ],        "temperature": 0.7, "max_tokens": 80 }),     success: function(response) {        // Handle the response from the API        const responseContent = response.choices[0].message.content;        callback(null, responseContent);      },      error: function(error) {        // Handle the error        callback(error);      }    }); }

// Function to summarize section function summarizeSection(sectionHeading) { // Extract the text underneath the section from tags var sectionText = ''; var sectionParent = sectionHeading.parent;

const openAPIKey = localStorage.getItem('openAPIKey'); if (!openAPIKey) { window.alert("Missing OpenAPI key. Please set"); return; }

console.log("Found API Key: ", openAPIKey); if (sectionParent.is('h2')) { var nextHeading = sectionParent.nextAll('h2, .mw-heading2, .mw-heading2, .ext-discussiontools-init-section').first; if (nextHeading.length === 0) { sectionText = sectionParent.parent.nextUntil('.mw-heading', 'p').text; } else { sectionText = sectionParent.nextUntil(nextHeading, 'p').text.trim; }   }

console.log("Found Section Text: ", sectionText); fetchSummaryFromOpenAPI(openAPIKey, sectionText, function(error, responseContent) {     if (error) {        console.log(error);      } else {        console.log("OpenAPI responded" + responseContent);        // Do further processing with the response content as a string      }    }); } });