Zuletzt bearbeitet vor 3 Tagen
von Margit Link-Rodrigue

Gadget-AutoImageAltText.js

Version vom 2. Oktober 2026, 14:00 Uhr von Margit Link-Rodrigue (Diskussion | Beiträge) (1 Version importiert)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
(()=>{"use strict";var t={685:function(t,e){var n=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))(function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(a,s)}u((r=r.apply(t,e||[])).next())})},r=this&&this.__generator||function(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(u){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!((o=(o=i.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=e.call(t,i)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,u])}}};function o(){return n(this,arguments,void 0,function(t){var e,n,o,i;return void 0===t&&(t=!1),r(this,function(r){switch(r.label){case 0:return r.trys.push([0,4,,5]),[4,mw.loader.using("mediawiki.util")];case 1:return r.sent(),e=mw.util.wikiScript("rest")+"/mws/v1/user-token/generate?withIssuer="+(t?"true":"false"),[4,fetch(e,{method:"GET",headers:{"Content-Type":"application/json; charset=utf-8"},credentials:"same-origin"})];case 2:if(!(n=r.sent()).ok)throw new Error("HTTP error! status: ".concat(n.status));return[4,n.json()];case 3:if((o=r.sent())&&o.value)return[2,o.value];throw new Error("No token value in response");case 4:if((i=r.sent())instanceof Error)throw new Error("Token generation failed: ".concat(i.message));throw new Error("Token generation failed: Unknown error");case 5:return[2]}})})}Object.defineProperty(e,"__esModule",{value:!0}),e.generateToken=o,e.initializeGlobal=function(){window.mws=window.mws||{},window.mws.tokenAuthenticator={generateToken:o}}},920:function(t,e,n){var r=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))(function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(a,s)}u((r=r.apply(t,e||[])).next())})},o=this&&this.__generator||function(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(u){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!((o=(o=i.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=e.call(t,i)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,u])}}};Object.defineProperty(e,"__esModule",{value:!0}),e.aiService=e.AIService=void 0;var i=n(685),a=function(){function t(){}return t.prototype.getEndpointUrl=function(){var t=mw.config.get("wgServer")||window.location.origin;return"".concat(t,"/_prompt")},t.prototype.request=function(t){return r(this,void 0,void 0,function(){var e,n,r,a,s;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,4,,5]),[4,(0,i.generateToken)(!0)];case 1:return e=o.sent(),n=void 0!==t.stream&&t.stream,r="".concat(this.getEndpointUrl(),"?stream=").concat(n,"&token=").concat(e),[4,fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"same-origin",body:JSON.stringify({ai_role:t.aiRole,instructions:t.instructions||[],query:t.query})})];case 2:if(!(a=o.sent()).ok)throw new Error("AI service returned ".concat(a.status,": ").concat(a.statusText));return[4,a.json()];case 3:return[2,o.sent()];case 4:if((s=o.sent())instanceof Error)throw new Error("AI service request failed: ".concat(s.message));throw new Error("AI service request failed: Unknown error");case 5:return[2]}})})},t.prototype.getCompletion=function(t,e,n){return r(this,void 0,void 0,function(){return o(this,function(r){switch(r.label){case 0:return[4,this.request({aiRole:t,query:e,instructions:n,stream:!1})];case 1:return[2,r.sent().answer]}})})},t.prototype.ask=function(t){return r(this,arguments,void 0,function(t,e){return void 0===e&&(e="You are a helpful assistant"),o(this,function(n){return[2,this.getCompletion(e,t)]})})},t}();e.AIService=a,e.aiService=new a},937:function(t,e,n){var r=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))(function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(a,s)}u((r=r.apply(t,e||[])).next())})},o=this&&this.__generator||function(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(u){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!((o=(o=i.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=e.call(t,i)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,u])}}};Object.defineProperty(e,"__esModule",{value:!0});var i=n(920),a=function(){function t(){this.buttonAdded=!1}return t.prototype.run=function(){mw.config.get("wgVisualEditorConfig")?this.init():console.log("AutoImageAltText: VisualEditor not available")},t.prototype.init=function(){var t=this;mw.hook("ve.activationComplete").add(function(){t.hookIntoMediaDialog()}),console.log("AutoImageAltText: Loaded and waiting for VE activation")},t.prototype.hookIntoMediaDialog=function(){var t=this;new MutationObserver(function(e){e.forEach(function(e){e.addedNodes.forEach(function(e){e instanceof HTMLElement&&t.checkForMediaDialog(e)})})}).observe(document.body,{childList:!0,subtree:!0})},t.prototype.checkForMediaDialog=function(t){var e=this;(t.classList.contains("ve-ui-mwMediaDialog")||t.querySelector(".ve-ui-mwMediaDialog"))&&setTimeout(function(){e.addAltTextButton()},500)},t.prototype.addAltTextButton=function(){var t=this;if(!this.buttonAdded&&!document.getElementById("auto-alt-text-button")){var e=document.querySelector(".ve-ui-mwMediaDialog-altText textarea");if(e){var n=document.querySelector(".ve-ui-mwMediaDialog-description-link");if(n){var r=document.createElement("button");r.id="auto-alt-text-button",r.textContent=mw.msg("bs-ai-alt-text-button"),r.type="button",r.style.cssText="\n\t\t\tbackground: #667eea;\n\t\t\tcolor: white;\n\t\t\tborder: none;\n\t\t\tborder-radius: 5px;\n\t\t\tpadding: 8px 16px;\n\t\t\tmargin-top: 8px;\n\t\t\tcursor: pointer;\n\t\t\tfont-weight: bold;\n\t\t\tfont-size: 13px;\n\t\t\ttransition: all 0.2s;\n\t\t",r.addEventListener("mouseenter",function(){r.style.background="#5568d3"}),r.addEventListener("mouseleave",function(){r.style.background="#667eea"}),r.addEventListener("click",function(){t.generateAltText(n.href,e)});var o=e.closest(".oo-ui-fieldLayout");o&&(o.appendChild(r),this.buttonAdded=!0,console.log("AutoImageAltText: Button added to media dialog"))}else console.log("AutoImageAltText: Description link not found")}else console.log("AutoImageAltText: Alt text input not found")}},t.prototype.generateAltText=function(t,e){return r(this,void 0,void 0,function(){var n,r,i,a,s,u,c,l;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,4,,5]),(n=t.match(/(?:File|Datei):(.+?)(?:\?|$)/))?(r=decodeURIComponent(n[1]),l=document.getElementById("auto-alt-text-button"),i=l.textContent,l.disabled=!0,l.textContent=mw.msg("bs-ai-alt-text-loading"),l.style.background="#999",mw.notify(mw.msg("bs-ai-alt-text-fetching"),{type:"info"}),[4,this.getImageUrl(r)]):(mw.notify(mw.msg("bs-ai-alt-text-filename-error"),{type:"error"}),[2]);case 1:if(!(a=o.sent()))throw new Error("Could not get image URL");return[4,this.fetchAndEncodeImage(a)];case 2:return s=o.sent(),[4,this.requestAltTextFromAI(s,r)];case 3:return u=o.sent(),e.value=u,e.dispatchEvent(new Event("input",{bubbles:!0})),mw.notify(mw.msg("bs-ai-alt-text-success"),{type:"success"}),l.disabled=!1,l.textContent=i||mw.msg("bs-ai-alt-text-button"),l.style.background="#667eea",[3,5];case 4:return c=o.sent(),console.error("Alt text generation failed:",c),mw.notify(mw.msg("bs-ai-alt-text-error",c.message),{type:"error"}),(l=document.getElementById("auto-alt-text-button"))&&(l.disabled=!1,l.textContent=mw.msg("bs-ai-alt-text-button"),l.style.background="#667eea"),[3,5];case 5:return[2]}})})},t.prototype.getImageUrl=function(t){return r(this,void 0,void 0,function(){var e,n;return o(this,function(r){switch(r.label){case 0:return[4,mw.loader.using("mediawiki.api")];case 1:return r.sent(),[4,(new mw.Api).get({action:"query",titles:"File:".concat(t),prop:"imageinfo",iiprop:"url",formatversion:2})];case 2:if(e=r.sent(),!(n=e.query.pages[0])||!n.imageinfo||!n.imageinfo[0])throw new Error("Image not found");return[2,n.imageinfo[0].url]}})})},t.prototype.fetchAndEncodeImage=function(t){return r(this,void 0,void 0,function(){var e,n;return o(this,function(r){switch(r.label){case 0:return[4,fetch(t)];case 1:if(!(e=r.sent()).ok)throw new Error("Failed to fetch image: ".concat(e.statusText));return[4,e.blob()];case 2:return n=r.sent(),[2,new Promise(function(t,e){var r=new FileReader;r.onloadend=function(){var e=r.result.split(",")[1];t(e)},r.onerror=e,r.readAsDataURL(n)})]}})})},t.prototype.requestAltTextFromAI=function(t,e){return r(this,void 0,void 0,function(){var n;return o(this,function(r){switch(r.label){case 0:return[4,i.aiService.request({aiRole:"You are an expert at creating accessible alt text for images",query:"Analyze this image (filename: ".concat(e,') and create a descriptive alt text. \nThe alt text should:\n- Be concise (1-2 sentences)\n- Describe what\'s visually important\n- Be useful for screen readers\n- Not include phrases like "image of" or "picture of"\n- Focus on the content and context\n\nImage data (base64): ').concat(t.substring(0,100),"...\n\nPlease provide ONLY the alt text, nothing else."),instructions:[{description:"Create concise, descriptive alt text"},{description:"Focus on accessibility"},{description:"Return only the alt text without any additional commentary"}],stream:!1})];case 1:return[2,((n=r.sent()).answer||n.response||"Unable to generate alt text").replace(/^["']|["']$/g,"").trim()]}})})},t}();mw.loader.using("mediawiki.api").then(function(){(new mw.Api).loadMessagesIfMissing(["bs-ai-alt-text-button","bs-ai-alt-text-loading","bs-ai-alt-text-filename-error","bs-ai-alt-text-fetching","bs-ai-alt-text-success","bs-ai-alt-text-error"]).then(function(){"loading"===document.readyState?document.addEventListener("DOMContentLoaded",function(){(new a).run()}):(new a).run()})})}},e={};!function n(r){var o=e[r];if(void 0!==o)return o.exports;var i=e[r]={exports:{}};return t[r].call(i.exports,i,i.exports,n),i.exports}(937)})();