99of9 / Entity-Explosion

A Chrome browser extension
GNU General Public License v3.0
26 stars 8 forks source link

Add custom user agent #20

Open lucaswerkmeister opened 4 months ago

lucaswerkmeister commented 4 months ago

Entity Explosion should set a user agent header with its requests (the fetch() calls in item-properties.js and background.js), identifying itself according to the user agent policy.

According to the web standards, you should be able to specify a custom User-Agent in requests (it is no longer a forbidden header name), and in Firefox this is indeed supported. However, Chrome/Chromium ignores the standard and continues to drop this header. Instead, you can send Api-User-Agent, which has no restrictions; it’s probably easiest to just do this in all the versions of the extension (rather than making the Firefox version send User-Agent and the Chrome version Api-User-Agent).

Example:

Api-User-Agent: Entity-Explosion (https://www.wikidata.org/wiki/Wikidata:Entity_Explosion)
99of9 commented 4 months ago

Thanks Lucas!

99of9 commented 3 months ago

I tried to add this (like the fetch code in https://foundation.wikimedia.org/wiki/Policy:User-Agent_policy): fetch('https://query.wikidata.org/sparql?query=' + encodedQuery, { method: 'GET', headers: new Header( { 'Accept': 'application/json', 'Api-User-Agent': 'Entity-Explosion (https://www.wikidata.org/wiki/Wikidata:Entity_Explosion)' } ) }) .then(

But I got this error: Access to fetch at 'https://query.wikidata.org/sparql?query='xxx' from origin 'chrome-extension://lkehkcgnfbfapdachbalabbhfkngbbem' has been blocked by CORS policy: Request header field api-user-agent is not allowed by Access-Control-Allow-Headers in preflight response.

A fairly old stack overflow suggests that the issue is at the other end and I should remove the user agent!? https://stackoverflow.com/questions/51575656/how-to-properly-cors-fetch-from-the-wikipedia-api

Any help would be appreciated, because I'm at a roadblock for now. @lucaswerkmeister

lucaswerkmeister commented 3 months ago

Hm, I didn’t realize query.wikidata.org didn’t allow Api-User-Agent. (Or, apparently, User-Agent.) Filed as T362570 now; in the meantime, I guess you can just leave out the header.

99of9 commented 3 months ago

Thanks for filing. I didn't realise this was unexplored territory!

lucaswerkmeister commented 3 months ago

Me neither :D

Do you also use the Wikidata API (/w/api.php, or /w/rest.php I suppose) anywhere, or only SPARQL/WDQS? If you use the normal API, you could at least add the user agent there…

99of9 commented 3 months ago

You're right, I've just turned that one back on in my development version. It is only used when the user invokes the extension from a Wikimedia site, and it just calls for the wikibase item QID, so I can't imagine it causing much load compared to the Wikidata queries.

ryankemper commented 3 weeks ago

User agent headers should be supported now: https://phabricator.wikimedia.org/T362570#9932326


BTW, neat extension!