I'm trying to write a script to remove duplicate links with the click of a button in the toolbar, yet when I set the function as the callback for the button, it runs the function on page load. This is what I've got:
function removeDuplicateLinks(){
var box =$('[id^=wpTextbox]'); var text = box.val(); var start=text.split(''); var i; for (i = 1;typeof(start[i]) !== 'undefined' ;i++){ console.log(start[i]); start[i] = start[i].split('')[0]; text = text.replace('/[[' + start[i] + ']]/', start[i]); } box.val(text); } if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(), } ); }
I've tried setting the callback to 'removeDuplicateLinks', removeDuplicateLinks, and I've even tried turning it into an anonymous function bound to a variable, which I then tried to pass as the callback. Am I misusing syntax, here?
I'd appreciate any help.
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik jfolvarcik@gmail.com wrote:
I'm trying to write a script to remove duplicate links with the click of a button in the toolbar, yet when I set the function as the callback for the button, it runs the function on page load. This is what I've got:
function removeDuplicateLinks(){
var box =$('[id^=wpTextbox]'); var text = box.val(); var start=text.split(''); var i; for (i = 1;typeof(start[i]) !== 'undefined' ;i++){
console.log(start[i]); start[i] = start[i].split('')[0]; text = text.replace('/[[' + start[i] + ']]/', start[i]); } box.val(text); } if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
} );
}
I've tried setting the callback to 'removeDuplicateLinks', removeDuplicateLinks, and I've even tried turning it into an anonymous function bound to a variable, which I then tried to pass as the callback. Am I misusing syntax, here?
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
-- Legoktm
Hi, LegoKTM, I tried your suggestion again, just to be sure, and I can confirm that this still does not load the callback properly. I set the value to callback: removeDuplicateLinks Now it does not load the function at all.
On Mon, Apr 21, 2014 at 8:37 PM, Lego KTM legoktm.wikipedia@gmail.comwrote:
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik jfolvarcik@gmail.com wrote:
I'm trying to write a script to remove duplicate links with the click of
a
button in the toolbar, yet when I set the function as the callback for
the
button, it runs the function on page load. This is what I've got:
function removeDuplicateLinks(){
var box =$('[id^=wpTextbox]'); var text = box.val(); var start=text.split(''); var i; for (i = 1;typeof(start[i]) !== 'undefined' ;i++){
console.log(start[i]); start[i] = start[i].split('')[0]; text = text.replace('/[[' + start[i] + ']]/', start[i]); } box.val(text); } if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
} );
}
I've tried setting the callback to 'removeDuplicateLinks', removeDuplicateLinks, and I've even tried turning it into an anonymous function bound to a variable, which I then tried to pass as the callback. Am I misusing syntax, here?
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
-- Legoktm
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
On 22-04-2014 05:35, Justin Folvarcik wrote:
I've tried setting the callback to 'removeDuplicateLinks', removeDuplicateLinks, and I've even tried turning it into an anonymous function bound to a variable, which I then tried to pass as the callback. Am I misusing syntax, here?
I have tried finding where mw.toolbar.addButton is defined, and closest I got is .../mediawiki.action.edit.js. It does not have a callback parameter, so I'm not even sure I have the right one. I may be woefully wrong... please point me to the right source file and doc page... This is truly frustrating!
What I *suspect* you are doing wrong is... using the 'callback:' parameter. I think this is the addButton's function callback. It stands to reason that it would be executed once the addButton function has done its work.
From what I have been able to discern from the badly organized documentation (again, I may be totally misguided), is that you want to use the 'action:' parameter instead.
Regards,
On 22 Apr 2014, at 02:37, Lego KTM legoktm.wikipedia@gmail.com wrote:
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik jfolvarcik@gmail.com wrote:
function removeDuplicateLinks(){
..
} if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
Indeed. That was the reason it ran on page load, because it was being invoked directly by your code when creating the button object.
To Justin: Please also change the code to extend the if-statement from `( wgAction === "edit" )` to be `( wgAction === "edit" && mw.toolbar )`.
On 22 Apr 2014, at 15:42, Erwin Dokter erwin@darcoury.nl wrote:
What I *suspect* you are doing wrong is... using the 'callback:' parameter. I think this is the addButton's function callback. It stands to reason that it would be executed once the addButton function has done its work.
From what I have been able to discern from the badly organized documentation (again, I may be totally misguided), is that you want to use the 'action:' parameter instead.
The mw.toolbar interface neither documents nor implements any such function. There is no such thing as button.callback or button.action, and for as long as I can remember, such feature never existed.
As for documentation, this particular method is quite well-documented:
https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.toolbar-method-...
If you need a callback, I'd recommend you re-enable the WikiEditor toolbar instead of the legacy classic toolbar, and use its API to add a button instead.
The WikiEditor API provides a wide range of features, including a callback.
— Krinkle
Am I correct in assuming that adding a check for mw.toolbar will help prevent the code from causing errors when in an edit view without a toolbar?
On Wed, Apr 23, 2014 at 7:14 AM, Krinkle krinklemail@gmail.com wrote:
On 22 Apr 2014, at 02:37, Lego KTM legoktm.wikipedia@gmail.com wrote:
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik jfolvarcik@gmail.com
wrote:
function removeDuplicateLinks(){
..
} if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
Indeed. That was the reason it ran on page load, because it was being invoked directly by your code when creating the button object.
To Justin: Please also change the code to extend the if-statement from `( wgAction === "edit" )` to be `( wgAction === "edit" && mw.toolbar )`.
On 22 Apr 2014, at 15:42, Erwin Dokter erwin@darcoury.nl wrote:
What I *suspect* you are doing wrong is... using the 'callback:'
parameter. I think this is the addButton's function callback. It stands to reason that it would be executed once the addButton function has done its work.
From what I have been able to discern from the badly organized
documentation (again, I may be totally misguided), is that you want to use the 'action:' parameter instead.
The mw.toolbar interface neither documents nor implements any such function. There is no such thing as button.callback or button.action, and for as long as I can remember, such feature never existed.
As for documentation, this particular method is quite well-documented:
https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.toolbar-method-...
If you need a callback, I'd recommend you re-enable the WikiEditor toolbar instead of the legacy classic toolbar, and use its API to add a button instead.
The WikiEditor API provides a wide range of features, including a callback.
— Krinkle
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Yes, it will ensure the code won't run if that particular toolbar isn't enabled. And it is also a safe guard for backwards compatibility (since the API used to have a different interface), and forwards compatibility (it might change).
Usually you wouldn't need such guard and instead use a dependency, but because you don't want to trigger a load of this module (you merely want to hook into it if the toolbar is there, you don't want to load another toolbar which is what adding a dependency would do).
— Krinkle
On 23 Apr 2014, at 14:21, Justin Folvarcik jfolvarcik@gmail.com wrote:
Am I correct in assuming that adding a check for mw.toolbar will help prevent the code from causing errors when in an edit view without a toolbar?
On Wed, Apr 23, 2014 at 7:14 AM, Krinkle krinklemail@gmail.com wrote:
On 22 Apr 2014, at 02:37, Lego KTM legoktm.wikipedia@gmail.com wrote:
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik jfolvarcik@gmail.com
wrote:
function removeDuplicateLinks(){
.. } if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
Indeed. That was the reason it ran on page load, because it was being invoked directly by your code when creating the button object.
To Justin: Please also change the code to extend the if-statement from `( wgAction === "edit" )` to be `( wgAction === "edit" && mw.toolbar )`.
On 22 Apr 2014, at 15:42, Erwin Dokter erwin@darcoury.nl wrote:
What I *suspect* you are doing wrong is... using the 'callback:'
parameter. I think this is the addButton's function callback. It stands to reason that it would be executed once the addButton function has done its work.
From what I have been able to discern from the badly organized
documentation (again, I may be totally misguided), is that you want to use the 'action:' parameter instead.
The mw.toolbar interface neither documents nor implements any such function. There is no such thing as button.callback or button.action, and for as long as I can remember, such feature never existed.
As for documentation, this particular method is quite well-documented:
https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.toolbar-method-...
If you need a callback, I'd recommend you re-enable the WikiEditor toolbar instead of the legacy classic toolbar, and use its API to add a button instead.
The WikiEditor API provides a wide range of features, including a callback.
— Krinkle
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
--
Justin Folvarcik *"When the power of love overcomes the love of power, the world will finally know peace."*-Jimi Hendrix _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Good to know. Also, I must have read the wrong documentation when I went to use addButton. Not sure where I got that callback parameter from. So instead I just attached an event handler to the button on click, and now it does what I want. Thanks for clearing up the confusion, Krinkle!
On Wed, Apr 23, 2014 at 6:53 PM, Krinkle krinklemail@gmail.com wrote:
Yes, it will ensure the code won't run if that particular toolbar isn't enabled. And it is also a safe guard for backwards compatibility (since the API used to have a different interface), and forwards compatibility (it might change).
Usually you wouldn't need such guard and instead use a dependency, but because you don't want to trigger a load of this module (you merely want to hook into it if the toolbar is there, you don't want to load another toolbar which is what adding a dependency would do).
— Krinkle
On 23 Apr 2014, at 14:21, Justin Folvarcik jfolvarcik@gmail.com wrote:
Am I correct in assuming that adding a check for mw.toolbar will help prevent the code from causing errors when in an edit view without a
toolbar?
On Wed, Apr 23, 2014 at 7:14 AM, Krinkle krinklemail@gmail.com wrote:
On 22 Apr 2014, at 02:37, Lego KTM legoktm.wikipedia@gmail.com wrote:
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik <
jfolvarcik@gmail.com>
wrote:
function removeDuplicateLinks(){
.. } if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
Indeed. That was the reason it ran on page load, because it was being invoked directly by your code when creating the button object.
To Justin: Please also change the code to extend the if-statement from
`(
wgAction === "edit" )` to be `( wgAction === "edit" && mw.toolbar )`.
On 22 Apr 2014, at 15:42, Erwin Dokter erwin@darcoury.nl wrote:
What I *suspect* you are doing wrong is... using the 'callback:'
parameter. I think this is the addButton's function callback. It stands
to
reason that it would be executed once the addButton function has done
its
work.
From what I have been able to discern from the badly organized
documentation (again, I may be totally misguided), is that you want to
use
the 'action:' parameter instead.
The mw.toolbar interface neither documents nor implements any such function. There is no such thing as button.callback or button.action,
and
for as long as I can remember, such feature never existed.
As for documentation, this particular method is quite well-documented:
https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.toolbar-method-...
If you need a callback, I'd recommend you re-enable the WikiEditor
toolbar
instead of the legacy classic toolbar, and use its API to add a button instead.
The WikiEditor API provides a wide range of features, including a
callback.
— Krinkle
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
--
Justin Folvarcik *"When the power of love overcomes the love of power, the world will finally know peace."*-Jimi Hendrix _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
wikitech-l@lists.wikimedia.org