Use ResourceLoader callback to retrieve hCaptcha config

The old method is returning null, results in error that makes
user stuck on the save your chagnes dialog in VE.

Bug: T311449
Change-Id: I124093fff4f7e69f7ec76cc6b9994d7939bc80f1
This commit is contained in:
alistair3149 2022-12-11 16:28:28 -05:00
parent 608dda6ab5
commit 2de530d092
No known key found for this signature in database
3 changed files with 23 additions and 30 deletions

View file

@ -46,21 +46,19 @@
}, },
"ResourceModules": { "ResourceModules": {
"ext.confirmEdit.hCaptcha.visualEditor": { "ext.confirmEdit.hCaptcha.visualEditor": {
"scripts": "ve-confirmedit-hCaptcha/ve.init.mw.HCaptchaSaveErrorHandler.js", "packageFiles": [
"ve-confirmedit-hCaptcha/ve.init.mw.HCaptchaSaveErrorHandler.js",
{
"name": "ve-confirmedit-hCaptcha/config.json",
"callback": "MediaWiki\\Extension\\ConfirmEdit\\hCaptcha\\Hooks\\ResourceLoaderHooks::getHCaptchaResourceLoaderConfig"
}
],
"targets": [ "targets": [
"desktop", "desktop",
"mobile" "mobile"
] ]
} }
}, },
"Hooks": {
"ResourceLoaderGetConfigVars": "resourceloader"
},
"HookHandlers": {
"resourceloader": {
"class": "MediaWiki\\Extension\\ConfirmEdit\\hCaptcha\\Hooks\\ResourceLoaderHooks"
}
},
"attributes": { "attributes": {
"VisualEditor": { "VisualEditor": {
"PluginModules": [ "PluginModules": [

View file

@ -5,25 +5,22 @@ declare( strict_types=1 );
namespace MediaWiki\Extension\ConfirmEdit\hCaptcha\Hooks; namespace MediaWiki\Extension\ConfirmEdit\hCaptcha\Hooks;
use Config; use Config;
use MediaWiki\MediaWikiServices; use MediaWiki\ResourceLoader as RL;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
class ResourceLoaderHooks implements ResourceLoaderGetConfigVarsHook { class ResourceLoaderHooks {
/** /**
* Adds extra variables to the global config * Passes config variables to ext.confirmEdit.hCaptcha.visualEditor ResourceLoader module.
* * @param RL\Context $context
* @param array &$vars Global variables object
* @param string $skin
* @param Config $config * @param Config $config
* @return void * @return array
*/ */
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void { public static function getHCaptchaResourceLoaderConfig(
$hCaptchaConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'hcaptcha' ); RL\Context $context,
if ( $hCaptchaConfig->get( 'CaptchaClass' ) === 'MediaWiki\\Extensions\\ConfirmEdit\\hCaptcha\\HCaptcha' ) { Config $config
$vars['wgConfirmEditConfig'] = [ ) {
'hCaptchaSiteKey' => $hCaptchaConfig->get( 'HCaptchaSiteKey' ), return [
'hCaptchaScriptURL' => 'https://hcaptcha.com/1/api.js', 'hCaptchaSiteKey' => $config->get( 'HCaptchaSiteKey' ),
]; 'hCaptchaScriptURL' => 'https://js.hcaptcha.com/1/api.js',
} ];
} }
} }

View file

@ -8,12 +8,11 @@ mw.loader.using( 'ext.visualEditor.targetLoader' ).then( function () {
ve.init.mw.HCaptchaSaveErrorHandler.static.getReadyPromise = function () { ve.init.mw.HCaptchaSaveErrorHandler.static.getReadyPromise = function () {
var onLoadFn = 'onHcaptchaLoadCallback' + Date.now(), var onLoadFn = 'onHcaptchaLoadCallback' + Date.now(),
deferred, config, scriptURL, params; deferred, scriptURL, params;
if ( !this.readyPromise ) { if ( !this.readyPromise ) {
deferred = $.Deferred(); deferred = $.Deferred();
config = mw.config.get( 'wgConfirmEditConfig' ); scriptURL = new mw.Uri( require( './config.json' ).hCaptchaScriptURL );
scriptURL = new mw.Uri( config.hCaptchaScriptURL );
params = { onload: onLoadFn, render: 'explicit' }; params = { onload: onLoadFn, render: 'explicit' };
scriptURL.query = $.extend( scriptURL.query, params ); scriptURL.query = $.extend( scriptURL.query, params );
@ -33,8 +32,7 @@ mw.loader.using( 'ext.visualEditor.targetLoader' ).then( function () {
ve.init.mw.HCaptchaSaveErrorHandler.static.process = function ( data, target ) { ve.init.mw.HCaptchaSaveErrorHandler.static.process = function ( data, target ) {
var self = this, var self = this,
config = mw.config.get( 'wgConfirmEditConfig' ), siteKey = require( './config.json' ).hCaptchaSiteKey,
siteKey = config.hCaptchaSiteKey,
$container = $( '<div>' ); $container = $( '<div>' );
// Register extra fields // Register extra fields