Skip to content

Commit

Permalink
v2.0.6
Browse files Browse the repository at this point in the history
Simplify the updating process;
Shrinkage the count string length of ZSCC ("ZSCC: 0000001" => "ZSCC:00001");
Remove state indicator of staleness;
  • Loading branch information
nico-zck committed Mar 24, 2022
1 parent 4a4f937 commit 23ebe04
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 107 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ New features:
- Fix url error when encounter special characters in the paper title;
- When updating many items, a random time delay is added, which may avoid Google Scholar traffic detection and reduce the frequency of reCAPTCHA;

---
v2.0.5
- Adaptation for Zotero 6.0;

v2.0.6
- Simplify the updating process;
- Shrinkage the count string length of ZSCC ("ZSCC: 0000001" => "ZSCC:00001");
- Remove state indicator of staleness;


---
---
# Zotero Scholar Citations (ZSC)
This is an add-on for Zotero, a research source management tool. The add-on automatically fetches numbers of citations of your Zotero items from Google Scholar and makes it possible to sort your items by the citations. Moreover, it allows batch updating the citations, as they may change over time.

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
version=$npm_package_version
mkdir -p build
zip -r build/zotero-scholar-citations-${version}-fx.xpi chrome/* chrome.manifest install.rdf
zip -r build/zotero-scholar-citations-${version}.xpi chrome/* chrome.manifest install.rdf
141 changes: 37 additions & 104 deletions chrome/content/zsc.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
let zsc = {
_captchaString: "",
_citedPrefixString: "Cited by ",
_citeCountStrLength: 7,
_extraPrefix: "ZSCC",
_extraEntrySep: " \n",
_noData: "NoCitationData",
// _searchblackList: new RegExp('[-+~*":]', 'g'),
_baseUrl: "https://scholar.google.com/",
_min_wait_time: 3000, // 3 seconds
_max_wait_time: 5000, // 5 seconds

_min_wait_time: 3000,
_max_wait_time: 5000,
_extraPrefix: "ZSCC:",
_citeCountStrLength: 5,
_noData: "NoData",
_extraEntrySep: "\n",
};

zsc._extraRegex = new RegExp(
"^(?:(?:" +
zsc._extraPrefix +
": )?)" +
"((?:(?:\\d{" +
zsc._citeCountStrLength +
"}|" +
zsc._noData +
")|(?:\\d{5}|No Citation Data))?)" +
"\\[?s?(\\d|)\\]?" +
"([^]*)$"
);
zsc._extraRegex = new RegExp(zsc._extraPrefix + ".*");

let isDebug = function () {
return (
Expand Down Expand Up @@ -125,19 +114,16 @@ zsc.processItems = function (items) {
if (!zsc.hasRequiredFields(item)) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
'skipping item "' +
"[scholar-citations] skipping item '" +
item.getField("title") +
'"' +
" it has either an empty title or is missing creator information"
"' it has either an empty title or is missing creator information"
);
continue;
}

if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"this retrieving will run in " +
"[scholar-citations] this retrieving will run in " +
time +
"milliseconds later."
);
Expand All @@ -149,10 +135,7 @@ zsc.processItems = function (items) {
function (item, citeCount) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
'Updating item "' +
item.getField("title") +
'"'
"[scholar-citations] Updating item '" + item.getField("title") + "'"
);
zsc.updateItem(item, citeCount);
}
Expand All @@ -164,74 +147,31 @@ zsc.processItems = function (items) {

zsc.updateItem = function (item, citeCount) {
let curExtra = item.getField("extra");
let matches = curExtra.match(zsc._extraRegex);
let newExtra = "";

if (citeCount >= 0) {
newExtra += zsc.buildCiteCountString(citeCount);
if (isDebug())
Zotero.debug("[scholar-citations] current extra field is: " + curExtra);

let newExtra = zsc.buildCiteCountString(citeCount);
if (zsc._extraRegex.test(curExtra)) {
// if already have ZSCC string
newExtra = curExtra.replace(zsc._extraRegex, newExtra);
if (isDebug())
Zotero.debug(
"[scholar-citations] " + "updating extra field with new cite count"
"[scholar-citations] replace old ZSCC with new string " + newExtra
);
} else {
if (matches[1] === "") {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"updating extra field that contains no zsc content"
);
newExtra += zsc.buildCiteCountString(citeCount);
} else if (
matches[1] === zsc._noData ||
matches[1] === "No Citation Data"
) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
'updating extra field that contains "no data"'
);
newExtra += zsc.buildCiteCountString(citeCount);
} else {
let oldCiteCount = parseInt(matches[1]);
newExtra += zsc.buildCiteCountString(oldCiteCount);
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"updating extra field that contains cite count"
);
}

if (!matches[2]) {
if (isDebug())
Zotero.debug("[scholar-citations] " + "marking extra field as stale");
newExtra += zsc.buildStalenessString(0);
} else {
if (isDebug())
Zotero.debug(
"[scholar-citations] " + "increasing staleness counter in extra field"
);
newExtra += zsc.buildStalenessString((parseInt(matches[2]) + 1) % 10);
}
}

if (/^\s\n/.test(matches[3]) || matches[3] === "") {
// do nothing, since the separator is already correct or not needed at all
} else if (/^\n/.test(matches[3])) {
newExtra += " ";
} else {
newExtra += zsc._extraEntrySep;
// if not have ZSCC string
newExtra = newExtra + zsc._extraEntrySep + curExtra;
if (isDebug())
Zotero.debug("[scholar-citations] add ZSCC to extra field " + newExtra);
}
newExtra += matches[3];

item.setField("extra", newExtra);

try {
item.saveTx();
} catch (e) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " + "could not update extra content: " + e
);
Zotero.debug("[scholar-citations] could not update extra content: " + e);
}
};

Expand All @@ -253,7 +193,7 @@ zsc.retrieveCitationData = function (item, cb) {
if (this.responseText.indexOf('class="gs_r gs_or gs_scl"') != -1) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " + "recieved non-captcha scholar results"
"[scholar-citations] received non-captcha scholar results"
);
cb(item, zsc.getCiteCount(this.responseText));
// check if response includes captcha
Expand All @@ -262,8 +202,7 @@ zsc.retrieveCitationData = function (item, cb) {
) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"received a captcha instead of a scholar result"
"[scholar-citations] received a captcha instead of a scholar result"
);
alert(zsc._captchaString);
if (typeof Zotero.openInViewer !== "undefined") {
Expand All @@ -279,8 +218,7 @@ zsc.retrieveCitationData = function (item, cb) {
// debug response text in other cases
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"neither got meaningful text or captcha, please check the following response text"
"[scholar-citations] neither got meaningful text or captcha, please check the following response text"
);
if (isDebug()) Zotero.debug(this.responseText);
alert("neither got meaningful text or captcha, please check it in log");
Expand All @@ -289,8 +227,7 @@ zsc.retrieveCitationData = function (item, cb) {
if (this.responseText.indexOf("www.google.com/recaptcha/api.js") == -1) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"could not retrieve the google scholar data. Server returned: [" +
"[scholar-citations] could not retrieve the google scholar data. Server returned: [" +
xhr.status +
": " +
xhr.statusText +
Expand All @@ -302,8 +239,7 @@ zsc.retrieveCitationData = function (item, cb) {
} else {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"received a captcha instead of a scholar result"
"[scholar-citations] received a captcha instead of a scholar result"
);
alert(zsc._captchaString);
if (typeof Zotero.openInViewer !== "undefined") {
Expand All @@ -319,8 +255,7 @@ zsc.retrieveCitationData = function (item, cb) {
} else if (this.readyState == 4) {
if (isDebug())
Zotero.debug(
"[scholar-citations] " +
"could not retrieve the google scholar data. Server returned: [" +
"[scholar-citations] could not retrieve the google scholar data. Server returned: [" +
xhr.status +
": " +
xhr.statusText +
Expand Down Expand Up @@ -379,15 +314,13 @@ zsc.padLeftWithZeroes = function (numStr) {
};

zsc.buildCiteCountString = function (citeCount) {
if (citeCount < 0) return zsc._extraPrefix + ": " + zsc._noData;
else
return (
zsc._extraPrefix + ": " + zsc.padLeftWithZeroes(citeCount.toString())
);
};

zsc.buildStalenessString = function (stalenessCount) {
return "[s" + stalenessCount + "]";
if (citeCount < 0) {
countString = zsc._extraPrefix + zsc._noData;
} else {
countString =
zsc._extraPrefix + zsc.padLeftWithZeroes(citeCount.toString());
}
return countString;
};

zsc.getCiteCount = function (responseText) {
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RDF:about="urn:mozilla:install-manifest"
em:id="[email protected]"
em:name="Zotero Scholar Citations"
em:version="2.0.5-SNAPSHOT"
em:version="2.0.6"
em:type="2"
em:creator="Anton Beloglazov, Max Kuehn, Nico"
em:description="Zotero plugin for auto-fetching numbers of citations from Google Scholar"
Expand Down

0 comments on commit 23ebe04

Please sign in to comment.