Skip to content

Commit

Permalink
close #4, close #6
Browse files Browse the repository at this point in the history
improve query url argument of author, add query option in preference.
  • Loading branch information
nico-zck committed Apr 5, 2022
1 parent 6817352 commit a040248
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 83 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,4 @@ typings/
build/
builds/
.DS_Store
package.json
package-lock.json
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
> For example, if a paper titled "Explaining and Exploiting Adversarial Examples" has a year of "2013" in Zotero, the original version will produce a URL that results in an incorrect query (https://scholar.google.com/scholar?q=Explaining+and+Harnessing+Adversarial+Examples&hl=en&as_sdt=0%2C5&as_ylo=2013&as_yhi=2013), while a looser range, such as 2012 - 2014, will hit the correct query (https://scholar.google.com/scholar?q=Explaining+and+Harnessing+Adversarial+Examples&hl=en&as_sdt=0%2C5&as_ylo=2012&as_yhi=2014).

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

Expand All @@ -19,6 +20,20 @@ v2.0.6
- Shrinkage the count string length of ZSCC ("ZSCC: 0000001" => "ZSCC:00001");
- Remove state indicator of staleness;

v2.1.0
- Add auto updating;
- Improve the query url with multiple authors;
- Add preference for base query url (beta);



## Development
1. Clone this source code.
2. Create a text file in the 'extensions' directory of your Zotero profile directory named after the extension id `[email protected]`. The file contents should be the absolute path to the root of your plugin source code directory, where your install.rdf file is located.
3. Open prefs.js in the Zotero profile directory in a text editor and delete the lines containing `extensions.lastAppBuildId` and `extensions.lastAppVersion`. Save the file and restart Zotero. This will force Zotero to read the extensions' directory and install your plugin from source, after which you should see it listed in Tools → Add-ons. This is only necessary once.
4. Run zotero with argments `-purgecaches -ZoteroDebug`, for example:
>./zotero.exe -purgecaches -ZoteroDebug

---
---
Expand Down
1 change: 0 additions & 1 deletion chrome.manifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
content zoteroscholarcitations chrome/content/

locale zoteroscholarcitations en-GB chrome/locale/en-GB/
locale zoteroscholarcitations en-US chrome/locale/en-US/
locale zoteroscholarcitations ru-RU chrome/locale/ru-RU/
locale zoteroscholarcitations it-IT chrome/locale/it-IT/
Expand Down
44 changes: 44 additions & 0 deletions chrome/content/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var Zotero = Components.classes["@zotero.org/Zotero;1"].getService(
Components.interfaces.nsISupports
).wrappedJSObject;

function checkScholarUrl() {
var boxUrl = document.getElementById("pref-zscc-scholar-url").value;
if (boxUrl.length == 0) {
Zotero.Prefs.set("extensions.zscc.scholarUrlVerified", false);
Zotero.Prefs.set("extensions.zscc.scholarUrl", "");
} else {
try {
if (boxUrl.startsWith("https://") && boxUrl.endsWith("/")) {
} else {
throw "Url should start with 'https:// and end with '/'";
}

testUrl = boxUrl + "schhp?hl=en";

let xhr = new XMLHttpRequest();
xhr.open("GET", testUrl, false);
xhr.send();

if (xhr.status == 200) {
if (
xhr.responseText.indexOf("Stand on the shoulders of giants") != -1
) {
// a valid url
alert("This site is valid and have been saved.");
Zotero.Prefs.set("extensions.zscc.scholarUrlVerified", true);
Zotero.Prefs.set("extensions.zscc.scholarUrl", boxUrl);
} else {
throw "This mirror site is not google scholar or not supports english language.";
}
} else {
throw "Cannot open this url, plaease check your url!";
}
} catch (err) {
// alert user
alert(err);
Zotero.Prefs.set("extensions.zscc.scholarUrlVerified", false);
Zotero.Prefs.set("extensions.zscc.scholarUrl", "");
}
}
}
29 changes: 29 additions & 0 deletions chrome/content/options.xul
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome:https://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome:https://zotero-platform/content/preferences.css"?>
<?xml-stylesheet href="chrome:https://zotero-platform-version/content/style.css"?>

<!DOCTYPE window SYSTEM "chrome:https://zoteroscholarcitations/locale/options.dtd">

<prefwindow id="zscc-perf" title="&zscc-preferences;"
xmlns="http:https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<prefpane label="&zscc-prefpane;" id="zscc-perfpane">
<preferences>
<preference id="pref-zscc-scholar-url" name="extensions.zscc.scholarUrl" type="string"/>
</preferences>

<groupbox>
<separator class="thin"/>
<label id="pref-zscc-scholar-url-label" value="&scholar-url-label;"/>
<description>&scholar-url-desc;</description>
<textbox id="pref-zscc-scholar-url" preference="pref-zscc-scholar-url"/>
<button id="pref-zscc-url-verify" label="&scholar-url-verify;" oncommand="checkScholarUrl();"/>
<separator class="thin"/>
</groupbox>


</prefpane>
<script src="options.js"/>
<script src="chrome:https://zotero/content/include.js"/>
</prefwindow>
28 changes: 10 additions & 18 deletions chrome/content/overlay.xul
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,23 @@

<!DOCTYPE overlay SYSTEM "chrome:https://zoteroscholarcitations/locale/overlay.dtd">

<overlay
id="zoteroscholarcitations-overlay"
<overlay id="zoteroscholarcitations-overlay"
xmlns="http:https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<stringbundleset>
<stringbundle
id="zoteroscholarcitations-bundle"
src="chrome:https://zoteroscholarcitations/locale/zsc.properties"/>
<stringbundle id="zoteroscholarcitations-bundle" src="chrome:https://zoteroscholarcitations/locale/zsc.properties"/>
</stringbundleset>

<script
type="application/x-javascript"
src="chrome:https://zoteroscholarcitations/content/zsc.js"/>
<script type="application/x-javascript" src="chrome:https://zoteroscholarcitations/content/zsc.js"/>

<popup id="zotero-collectionmenu">
<menuitem
id="zotero-collectionmenu-scholarcitations"
label="&zotero.scholarcitations.updateCol.label;"
oncommand="Zotero.ScholarCitations.updateCollectionMenuEntry()"/>
<menuitem id="zotero-collectionmenu-scholarcitations" label="&zotero.scholarcitations.updateCol.label;" oncommand="Zotero.ScholarCitations.updateCollectionMenuEntry()"/>
</popup>

<popup
id="zotero-itemmenu">
<menuitem
id="zotero-itemmenu-scholarcitations"
label="&zotero.scholarcitations.update.label;"
oncommand="Zotero.ScholarCitations.updateItemMenuEntries()"/>
<popup id="zotero-itemmenu">
<menuitem id="zotero-itemmenu-scholarcitations" label="&zotero.scholarcitations.update.label;" oncommand="Zotero.ScholarCitations.updateItemMenuEntries()"/>
</popup>

<menupopup id="menu_ToolsPopup">
<menuitem id="zotero-scholarcitations-options" insertafter="menu_preferences" label="&zotero.scholarcitations.options.label;" oncommand="Zotero.ScholarCitations.openPreferenceWindow();"/>
</menupopup>
</overlay>
70 changes: 56 additions & 14 deletions chrome/content/zsc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
function isDebug() {
return (
typeof Zotero != "undefined" &&
typeof Zotero.Debug != "undefined" &&
Zotero.Debug.enabled
);
}

function getPref(pref) {
return Zotero.Prefs.get("extensions.zscc." + pref);
}

//######################################################################
let zsc = {
_captchaString: "",
_citedPrefixString: "Cited by ",
// _searchblackList: new RegExp('[-+~*":]', 'g'),
_baseUrl: "https://scholar.google.com/",
// _baseUrl: "https://scholar.google.com/",
_min_wait_time: 3000, // 3 seconds
_max_wait_time: 5000, // 5 seconds

Expand All @@ -12,15 +25,7 @@ let zsc = {
_extraEntrySep: "\n",
};

zsc._extraRegex = new RegExp(zsc._extraPrefix + ".{0,20}");

let isDebug = function () {
return (
typeof Zotero != "undefined" &&
typeof Zotero.Debug != "undefined" &&
Zotero.Debug.enabled
);
};
zsc._extraRegex = new RegExp(zsc._extraPrefix + ".{0,20}" + zsc._extraEntrySep);

zsc.init = function () {
let stringBundle = document.getElementById("zoteroscholarcitations-bundle");
Expand Down Expand Up @@ -184,7 +189,7 @@ zsc.retrieveCitationData = function (item, cb) {
let citeCount;
let xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
// xhr.responseType = "document";
// xhr.responseType = "document"; // will return a HTMLDocument instead of text
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// // debug on response text
Expand Down Expand Up @@ -295,8 +300,8 @@ zsc.retrieveCitationData = function (item, cb) {

zsc.generateItemUrl = function (item) {
let url =
zsc._baseUrl +
"scholar?hl=en&q=" +
zsc.getBaseUrl() +
"scholar?hl=en&as_q=" +
// + zsc.cleanTitle(item.getField('title'))
item.getField("title") +
"&as_epq=&as_occt=title&num=1";
Expand All @@ -316,7 +321,7 @@ zsc.generateItemUrl = function (item) {
let year = parseInt(item.getField("year"));
if (year) {
// set a small range of year instead of an exact number
url += "&as_ylo=" + (year - 2) + "&as_yhi=" + (year + 2);
url += "&as_ylo=" + (year - 1) + "&as_yhi=" + (year + 1);
}

return encodeURI(url);
Expand Down Expand Up @@ -364,6 +369,40 @@ zsc.getCiteCount = function (responseText) {
}
};

zsc.openPreferenceWindow = function (paneID, action) {
var io = { pane: paneID, action: action };
window.openDialog(
"chrome:https://zoteroscholarcitations/content/options.xul",
"zotero-scholarcitations-options",
"chrome,titlebar,toolbar,centerscreen" +
Zotero.Prefs.get("browser.preferences.instantApply", true)
? "dialog=no"
: "modal",
io
);
};

zsc.getBaseUrl = function () {
defaultUrl = "https://scholar.google.com/";
userUrl = getPref("scholarUrl");

if (userUrl != null && userUrl.length > 0 && getPref("scholarUrlVerified")) {
if (isDebug) {
Zotero.debug(
"[scholar-citations] will use a user specified base url: " + userUrl
);
}
return userUrl;
} else {
if (isDebug) {
Zotero.debug(
"[scholar-citations] will use the default base url: " + defaultUrl
);
}
return defaultUrl;
}
};

if (typeof window !== "undefined") {
window.addEventListener(
"load",
Expand All @@ -384,6 +423,9 @@ if (typeof window !== "undefined") {
window.Zotero.ScholarCitations.updateItemMenuEntries = function () {
zsc.updateItemMenuEntries();
};
window.Zotero.ScholarCitations.openPreferenceWindow = function () {
zsc.openPreferenceWindow();
};
}

if (typeof module !== "undefined") module.exports = zsc;
3 changes: 0 additions & 3 deletions chrome/locale/en-GB/overlay.dtd

This file was deleted.

2 changes: 0 additions & 2 deletions chrome/locale/en-GB/zsc.properties

This file was deleted.

5 changes: 5 additions & 0 deletions chrome/locale/en-US/options.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!ENTITY zscc-preferences "ZSCC Preferences">
<!ENTITY zscc-prefpane "ZSCC Preferences">
<!ENTITY scholar-url-label "Scholar Mirror Site (BETA)">
<!ENTITY scholar-url-desc "The format should start with 'https://' and end with '/', e.g. https://scholar.google.com/. Leave this blank will use the official site.">
<!ENTITY scholar-url-verify "Verify URL">
1 change: 1 addition & 0 deletions chrome/locale/en-US/overlay.dtd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!ENTITY zotero.scholarcitations.update.label "Update citation(s)">
<!ENTITY zotero.scholarcitations.updateCol.label "Update citations">
<!ENTITY zotero.scholarcitations.updateAll.label "Update all citations">
<!ENTITY zotero.scholarcitations.options.label "ZSCC Preferences...">
89 changes: 45 additions & 44 deletions install.rdf
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
<?xml version="1.0"?>
<RDF:RDF
xmlns:em="http:https://www.mozilla.org/2004/em-rdf#"
xmlns:NC="http:https://home.netscape.com/NC-rdf#"
xmlns:RDF="http:https://www.w3.org/1999/02/22-rdf-syntax-ns#">
<RDF:Description
RDF:about="rdf:#$x61SL3"
em:id="{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"
em:minVersion="2.0"
em:maxVersion="20.*"/>
<RDF:Description
RDF:about="urn:mozilla:install-manifest"
em:id="[email protected]"
em:name="Zotero Scholar Citations"
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"
em:homepageURL="https://github.com/nico-zck/zotero-scholar-citations">
<em:type>2</em:type>
<em:targetApplication RDF:resource="rdf:#$x61SL3"/>
<em:targetApplication>
<Description>
<em:id>[email protected]</em:id>
<em:minVersion>5.0.66</em:minVersion>
<em:maxVersion>6.*</em:maxVersion>
</Description>
</em:targetApplication>
<em:localized>
<Description>
<em:locale>ru-RU</em:locale>
<em:name>Zotero Scholar Citations</em:name>
<em:description>Плагин для Zotero для автоматической выборки числа цитирований из Google Scholar.</em:description>
</Description>
</em:localized>
<em:localized>
<Description>
<em:locale>it-IT</em:locale>
<em:name>Zotero Scholar Citations</em:name>
<em:description>Plugin di Zotero per il recupero automatico del numero di citazioni in Google Scholar.</em:description>
</Description>
</em:localized>
</RDF:Description>
</RDF:RDF>
<RDF xmlns="http:https://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http:https://www.mozilla.org/2004/em-rdf#">

<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:name>Zotero Scholar Citations</em:name>
<em:version>2.1.0</em:version>
<em:type>2</em:type>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:creator>Nico</em:creator>
<em:homepageURL>https://github.com/nico-zck/zotero-scholar-citations</em:homepageURL>
<em:optionsURL>chrome:https://zoteroscholarcitations/content/options.xul</em:optionsURL>
<em:updateURL>https://github.com/nico-zck/zotero-scholar-citations/master/update.rdf</em:updateURL>

<em:targetApplication>
<Description>
<em:id>[email protected]</em:id>
<em:minVersion>5.0.0</em:minVersion>
<em:maxVersion>6.0.*</em:maxVersion>
</Description>
</em:targetApplication>

<em:localized>
<Description>
<em:locale>en-US</em:locale>
<em:name>Zotero Scholar Citations</em:name>
<em:description>Advanced PDF management for Zotero</em:description>
</Description>
</em:localized>
<em:localized>
<Description>
<em:locale>ru-RU</em:locale>
<em:name>Zotero Scholar Citations</em:name>
<em:description>Плагин для Zotero для автоматической выборки числа цитирований из Google Scholar.</em:description>
</Description>
</em:localized>
<em:localized>
<Description>
<em:locale>it-IT</em:locale>
<em:name>Zotero Scholar Citations</em:name>
<em:description>Plugin di Zotero per il recupero automatico del numero di citazioni in Google Scholar.</em:description>
</Description>
</em:localized>
</Description>
</RDF>
Loading

0 comments on commit a040248

Please sign in to comment.