Wikipedia talk:WikiProject JavaScript: Difference between revisions
Content deleted Content added
→Importscript vs just putting code in common.js: new section |
|||
Line 605:
:{{u|NewsAndEventsGuy}}, I've deleted the page for you. You can tag with {{t|db-g7}} as DannyS712 suggested to get the page deleted. [[User:Galobtter|Galobtter]] ([[User talk:Galobtter|pingó mió]]) 05:44, 12 August 2019 (UTC)
::: Thank you both! I forgot about db-g7, thanks for the reminder. [[User:NewsAndEventsGuy|NewsAndEventsGuy]] ([[User talk:NewsAndEventsGuy|talk]]) 10:49, 12 August 2019 (UTC)
== Importscript vs just putting code in common.js ==
Back in the [[Cretaceous]] I was an experienced programmer. Javascript (and wiki userscripts) are new to me. I hope this is a simple question with a simple explanation. The question is
:Why on earth does the same code behave differently when it is ''part'' of common.js, as opposed to being imported from a separate file?
So that's it. If it's a simple question you can stop reading now. In case it isn't simple, here is my story
{{od}}
1. Seeking education about harv citation, [[Help:Citation tools]] steered me to [[User:Ucucha/HarvErrors.js]]<br>
2. At the time I had no common.js page<br>
3. The other editor's directions say to use importscript, but I like to keep things tight so instead of importing I created a common.js for myself and copied the contents of this other editor's user script into it. After all, if it works when importing from his/her userspace, why not just put it directly into my common.js where I am most likely to keep an eye on it?<br>
4. It worked great until I noticed the "show" button on one article talk page was not working.<br>
5. I isolated the onset of this behavior to [https://en.wikipedia.org/w/index.php?title=Talk:Global_warming&diff=908531695&oldid=908525653 this DIFF]. Subsequent testing identified that diff's use of [[Template:harvnb]] as the culprit. In particular, the harvnb cites had no associated reference in the bibliography. In harv citation speak, the diff created pipes but no anchors. That creates a TRUE condition in the first test in [[User:Ucucha/HarvErrors.js]] (which I had copied to my common.js)<br>
6. For convenience, I created my own test user subpage with a harvnb citation but no bibliography. To my surprise I could not reproduce the error. <br>
7. Back at the isolated diff described above, I finally noticed there were ''two'' such citations in a single paragraph. So I created a ''second'' test user subpage. The two test pages are
:::*[[User:NewsAndEventsGuy/ShowButtonTest-DifferentParagraphs|Harv errors in different paragraphs (test page)]]
:::*[[User:NewsAndEventsGuy/ShowButtonTest-SameParagraph|Harv errors in same paragraph (test page)]]
8. Back at common.js, I started pruning to isolate the snippet that produced this behavior. Eventually I pruned it down as far as I could, and I replaced the contents of the conditional IF-statment to force a TRUE condition.
<syntaxhighlight lang="javascript">
mw.hook( 'wikipage.content' ).add( function( $content ) {
var href,
links = $content.find( 'a[href^="#CITEREF"]' );
links.each( function (i, elem) {
href = elem.getAttribute( 'href' ).substring(1); //skip the #
if ( 1 < 3) {
elem.parentNode.innerHTML += "harv error";}
} );
});
</syntaxhighlight><br>
9. With that code in my common.js, the show button in
:::*[[User:NewsAndEventsGuy/ShowButtonTest-SameParagraph|Harv errors in same paragraph (test page)]]
is disabled but the show button in
:::*[[User:NewsAndEventsGuy/ShowButtonTest-DifferentParagraphs|Harv errors in different paragraphs (test page)]]
works just fine.<br>
10. '''However the weird thing''' is with this same code in a separate user page, [[User:NewsAndEventsGuy/test.js]] and changing common.js so it just has a single "importscript" line, the show button works fine on both test pages. <br>
11. Incidentally, when I tweaked the conditional statement to force a FALSE condition, the show button worked fine on both test pages, whether the code was in common.js or imported form test.js. <br>
12. To summarize the factors that appear necessary to kill the show button they are
::: A. This snippet of code has to be in common.js not imported from elsewhere
::: B. There must be ''two'' harv citations without bibliographic references
::: C. They must be in the ''same paragraph''
13. I have an inkling of what's going on with (B) and (C). What truly has me stumped is (A).
14. Going full circle, I verified that the original Harv Errors script works fine if I make a separate user file in my own userspace, paste it there, and then import it from myself. It's only when I paste the original directly into common.js that this problem appears.
{{od}}
'''Bottom line question'''....Why on earth does the same code behave differently when it is ''part'' of common.js, as opposed to being imported from a separate file?
Hopefully there is a simple answer having to do with what happens when you "import", but if this really is an unexpected behavior....then..... one might ask who cares? Just import it from a separate file and be happy. Indeed, that's what I plan to do. HOWEVER.... if it really is unexpected, then something else as-yet-unnoticed might be going on too so I thought I would make this report and maybe learn something at the same time.
Thanks, please be kind to the javascript newbie.
[[User:NewsAndEventsGuy|NewsAndEventsGuy]] ([[User talk:NewsAndEventsGuy|talk]]) 13:13, 12 August 2019 (UTC)
|