Wikipedia talk:User scripts/Guide: Difference between revisions

Content deleted Content added
User settings: personally still recommend declaring a global variable explicitly
Legobot (talk | contribs)
m Bot: Fixing lint errors, replacing obsolete HTML tags: <tt> (5x)
 
(14 intermediate revisions by 5 users not shown)
Line 19:
 
Today User:Zvn edited section "Removing elements". He changed this:
:''To move an element simply attach it in another place with <ttcode>appendChild()</ttcode> or <ttcode>insertBefore()</ttcode>.''
to this:
:''To move an element simply attach it in another place with <ttcode>appendChild()</ttcode> or <ttcode>insertBefore()</ttcode> and remove the original element with <ttcode>removeChild()</ttcode>.''
I am just a beginner javacoder, but I think that is wrong. There is no need to remove the item from the old position manually, that happens automatically when one has inserted it in a new position.
 
Line 67:
// ... set default values ...
};</syntaxhighlight> [[User:Isaacl|isaacl]] ([[User talk:Isaacl|talk]]) 17:43, 11 February 2021 (UTC)
 
== The most famous solution is Logan? ==
 
Per the page:
 
"Some browsers allow you to automatically execute your JavaScript code on specific web pages. This way you don't even have to be logged in.
 
The most famous solution is Logan and equivalents for other browsers."
 
I have never heard of Logan. The most famous one I know is Greasemonkey and Tampermonkey. I googled "logan browser script" but could not find anything that looks related. Can someone add a link to this Logan to clarify what is meant here? Or if it is out-of-date, can I just remove it and maybe add Greasemonkey and Tampermonkey? [[User:Betty|Betty]] ([[User talk:Betty|talk]]) 09:52, 7 September 2021 (UTC)
:Good points, I agree. {{done}}. I replaced Logan with Tampermonkey. –[[User:Novem Linguae|<span style="color:limegreen">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 10:04, 7 September 2021 (UTC)
::@[[User:Novem Linguae|Novem Linguae]] That was swift. :) BTW, I'm still a little curious about what Logan is... [[User:Betty|Betty]] ([[User talk:Betty|talk]]) 11:32, 7 September 2021 (UTC)
 
== Ajax and cross-origin requests ==
 
Hi! The documentation says, in the Ajax section, that "AJAX scripts cannot reach a page on a different server (for example, google.ca or en.wikisource.org from en.wikipedia.org". I'm writing a user script that requires making a query to a tool hosted in Toolforge. I thought this would not be possible (given what the documentation says), but I found that `$.ajax` is being able to fetch resources from it, as long as other server includes the appropriate CORS Allow-Origin headers. Am I misinterpreting the documentation? Or has this changed lately? Thanks! [[User:Diegodlh|Diegodlh]] ([[User talk:Diegodlh|talk]]) 16:02, 12 April 2022 (UTC)
:{{ping|Diegodlh}} I'm not sure where I heard about this, but it seems that CORS has not affected us yet; currently it is in logging mode. Furthermore, all Wikimedia domains are allowed. Try going to [[:testwiki:|Test Wikipedia]], and you will see this red error appearing in your browser console:
:{{small|{{error|1=[Report Only] Refused to load the script '[URL]' because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' blob: 'self' meta.wikimedia.org *.wikimedia.org *.wikipedia.org *.wikinews.org *.wiktionary.org *.wikibooks.org *.wikiversity.org *.wikisource.org wikisource.org *.wikiquote.org *.wikidata.org *.wikivoyage.org *.mediawiki.org 'unsafe-inline'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.}}}}
:[[User:NguoiDungKhongDinhDanh|<span style="font-family:Monotype Corsiva;background-image:linear-gradient(90deg,red,yellow,cyan);color:transparent;background-clip:text;-webkit-background-clip:text">'''NguoiDungKhongDinhDanh'''</span>]] 16:26, 12 April 2022 (UTC)
::Thanks, @[[User:NguoiDungKhongDinhDanh|NguoiDungKhongDinhDanh]]. I tried using the user script (which makes a request to toolforge.org) in Test Wikipedia and I'm getting a different error (although the request does succeed, as you say): `Content Security Policy: The page’s settings observed the loading of a resource at <nowiki>https://web2cit.toolforge.org/</nowiki> (“default-src”). A CSP report is being sent.`. The warning does not appear if the user script sends a request to en.wikipedia.org, for example.
::I wonder whether CSP will be enforced in the future (I hope *.toolforge.org is allowed as well). This may not be what the Ajax section of the User scripts guide is referring to, though: "AJAX scripts cannot reach a page on a different server (for example, google.ca or en.wikisource.org from en.wikipedia.org". I wonder whether it's safe to remove this from the docs. [[User:Diegodlh|Diegodlh]] ([[User talk:Diegodlh|talk]]) 18:43, 12 April 2022 (UTC)
:::{{ping|Diegodlh}} Unfortunately, toolforge may not be allowed according to {{phab|T304107#7787150}} and {{phab|T304151}}. For the time being, I think you can [[WP:BOLD|be bold]] and remove that sentence. [[User:NguoiDungKhongDinhDanh|<span style="font-family:Monotype Corsiva;background-image:linear-gradient(90deg,red,yellow,cyan);color:transparent;background-clip:text;-webkit-background-clip:text">'''NguoiDungKhongDinhDanh'''</span>]] 07:14, 13 April 2022 (UTC)
 
== Weird syntax ==
 
Is there a reason the code is written <code>document.editform.wpTextbox1.value = "{" + "{wikify}}\n\n" + document.editform.wpTextbox1.value;</code>? Why are the two left brackets separated? [[User:Dimpizzy|Dimpizzy]] ([[User talk:Dimpizzy|talk]]) 17:42, 20 January 2023 (UTC)
 
:Probably to keep {{t|wikify}}'s content from getting substituted into the code, which can happen if you don't wrap your scripts in
<syntaxhighlight lang="js">//<nowiki>
 
scriptGoesHere();
 
//</nowiki></syntaxhighlight>
 
:–[[User:Novem Linguae|<span style="color:limegreen">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 17:45, 20 January 2023 (UTC)
::Ah ok, I just saw the sentence in the page after the code explaining why as well, and I came back here to delete this topic, but you were too quick! [[User:Dimpizzy|Dimpizzy]] ([[User talk:Dimpizzy|talk]]) 17:47, 20 January 2023 (UTC)
:::Hehe :) –[[User:Novem Linguae|<span style="color:limegreen">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 20:24, 20 January 2023 (UTC)