Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MV3: Support CSL import from all .csl gitee pages #507

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/browserExt/contentTypeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Zotero.ContentTypeHandler = {
mv3CSLWhitelistRegexp: {
"https://www.zotero.org/styles/\\1": /https?:\/\/(?:www\.)zotero\.org\/styles\/?#importConfirm=(.*)$/,
"https://raw.githubusercontent.com/\\1/\\2": /https?:\/\/github\.com\/([^/]*\/[^/]*)\/[^/]*\/([^.]*.csl)#importConfirm$/,
"https://gitee.com/\\1/raw/\\2": /https?:\/\/gitee\.com\/([^/]+\/[^/]+)\/blob\/(.+\.csl)$/
},
ignoreURL: new Set(),

Expand Down Expand Up @@ -137,15 +138,14 @@ Zotero.ContentTypeHandler = {
},

_isImportableStyle: function (url, contentType) {
const URI = new URL(url);
// Offer to install CSL by Content-Type
if (Zotero.ContentTypeHandler.cslContentTypes.has(contentType)) {
return true;
}
// Offer to install CSL if URL path ends with .csl and host is allowed
else if (URI.pathname.match(/\.csl$/)) {
else if (/\.csl$/i.test(url)) {
let hosts = Zotero.Prefs.get('allowedCSLExtensionHosts');
if (Array.isArray(hosts) && hosts.includes(URI.hostname)) {
if (Array.isArray(hosts) && hosts.some(host => new RegExp(host).test(url))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using regular expressions to test URLs is more compatible, but some variable names may need to be renamed

return true;
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/browserExt/styleInterceptRules.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,19 @@
"regexFilter": "^https://raw\\.githubusercontent\\.com/([^/]*/[^/]*)/([^/]*/[^#?]+.csl)$",
"resourceTypes": ["main_frame"]
}
},
{
"id": 3,
"priority": 1,
"action": {
"type": "redirect",
"redirect": {
"regexSubstitution": "https://gitee.com/\\1/blob/\\2"
}
},
"condition": {
"regexFilter": "^https://gitee\\.com/([^/]+/[^/]+)/raw/(.+\\.csl)$",
"resourceTypes": ["main_frame"]
}
}
]
2 changes: 1 addition & 1 deletion src/common/zotero.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ Zotero.Prefs = new function() {
"connector.url": 'http://127.0.0.1:23119/',
"capitalizeTitles": false,
"interceptKnownFileTypes": true,
"allowedCSLExtensionHosts": ["raw.githubusercontent.com"],
"allowedCSLExtensionHosts": ["^https://raw\\.githubusercontent\\.com/", "^https://gitee\\.com/.+/raw/"],
"allowedInterceptHosts": [],
"firstUse": true,
"firstSaveToServer": true,
Expand Down