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

Big update #42

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zotero-google-scholar-citation-count",
"version": "4.1.1",
"version": "5.0.0",
"bugs": {
"url": "https://github.com/justinribeiro/zotero-google-scholar-citation-count/issues"
},
Expand Down
108 changes: 43 additions & 65 deletions src/gscc.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,6 @@ $__gscc.util = {
xhr.send();
});
},
/**
* Get a random number
* @param {number} min
* @param {number} max
* @return {number}
*/
randomInteger: function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
/**
* Checks to validate whether the source code has a recaptcha within it
* @param {string} source source code to parse and search for recaptcha
Expand Down Expand Up @@ -204,17 +195,12 @@ $__gscc.app = {
* @private
*/
__noData: 'NoCitationData',
/**
* API endpoint for Google Scholar
* @private
*/
__apiEndpoint: 'https://scholar.google.com/',
/**
* Default String search in Google Scholar,
* will override based on locale
* @private
*/
__citedByPrefix: 'Cited by',
__citedByPrefix: '被引用次数:',
/**
* My own marker for init; not for general use
* @private
Expand All @@ -225,13 +211,11 @@ $__gscc.app = {
* @private
*/
__preferenceDefaults: {
useRandomWait: true,
randomWaitMinMs: 1000,
randomWaitMaxMs: 5000,
useFuzzyMatch: false,
useSearchTitleFuzzyMatch: false,
useSearchAuthorsMatch: true,
useDateRangeMatch: false,
mirrorurl:'https://scholar.google.com/',
},
/**
* Initialize our world.
Expand All @@ -242,11 +226,23 @@ $__gscc.app = {
this.id = id;
this.version = version;
this.rootURI = rootURI;

// sanity
$__gscc.app.__initialized = true;

$__gscc.debugger.info(`Init() Complete! ${this.rootURI}`);

// Add event listener for new items
Zotero.Notifier.registerObserver($__gscc.app.notifierCallback, ['item']);
},

notifierCallback: {
notify: async function(event, type, ids, extraData) {
if (event === 'add' && type === 'item') {
const newItems = await Zotero.Items.getAsync(ids);
await $__gscc.app.updateItemMenuEntries(newItems);
}
}
},

main: async function () {
Expand Down Expand Up @@ -287,6 +283,7 @@ $__gscc.app = {
await Zotero.ItemTreeManager.registerColumns({
dataKey: 'gsccCount',
label: columnLabel,
flex:0.4,
pluginID: '[email protected]',
dataProvider: (item, dataKey) => {
const data = item.getField('extra');
Expand All @@ -300,17 +297,19 @@ $__gscc.app = {
* @param {String} extraString
*/
setFieldFromExtra: function (extraString) {
let count = 0;
let count = '';
if (extraString.startsWith(this.__extraEntryPrefix)) {
try {
const regex = new RegExp(
String.raw`${this.__extraEntryPrefix}:(\s*\d+)`,
'g',
);
// meh
const match = extraString.match(regex)[0];
const split = match.split(':')[1].trim();
count = parseInt(split);
const match = extraString.match(regex);
if (match){
const split = match[0].split(':')[1].trim();
count = parseInt(split);
}
} catch {
// dead case for weird behavior
}
Expand Down Expand Up @@ -384,18 +383,24 @@ $__gscc.app = {
window.alert(unSupportedEntryTypeString);
return;
},
updateItemMenuEntries: async function () {

updateItemMenuEntries: async function (items = null) {
const zoteroPane = $__gscc.app.getActivePane();
const window = Zotero.getMainWindow();

if (!zoteroPane.canEditLibrary()) {
const permissionAlertString = await window.document.l10n.formatValue(
'gscc-lackPermissions',
);
window.alert(permissionAlertString);
return;
}
await this.processItems(zoteroPane.getSelectedItems());

if (!items) {
items = zoteroPane.getSelectedItems();
}

await this.processItems(items);
},
updateGroup: async function () {
const window = Zotero.getMainWindow();
Expand All @@ -416,29 +421,6 @@ $__gscc.app = {
* @param {ZoteroGenericItem[]} items
*/
processItems: async function (items) {
const useQueue = Zotero.Prefs.get(
'extensions.zotero.gscc.useRandomWait',
$__gscc.app.__preferenceDefaults.useRandomWait,
);

let queueMinWaitMs;
let queueMaxWaitMs;

$__gscc.debugger.info(`Use Queue: ${useQueue}`);

if (useQueue) {
queueMinWaitMs = Zotero.Prefs.get(
'extensions.zotero.gscc.randomWaitMinMs',
$__gscc.app.__preferenceDefaults.randomWaitMinMs,
);
queueMaxWaitMs = Zotero.Prefs.get(
'extensions.zotero.gscc.randomWaitMaxMs',
$__gscc.app.__preferenceDefaults.randomWaitMaxMs,
);

$__gscc.debugger.info(`Min: ${queueMinWaitMs} Max: ${queueMaxWaitMs}`);
}

/**
* @param {number} index
* @param {ZoteroGenericItem} item
Expand All @@ -451,19 +433,8 @@ $__gscc.app = {
)}': empty title or missing creator information'`,
);
} else {
// check the prefs in case user override, don't use it on the first item
// either way
if (useQueue && index > 0) {
const queueTime = $__gscc.util.randomInteger(
queueMinWaitMs,
queueMaxWaitMs,
);

$__gscc.debugger.info(`queued for ${queueTime} ms later.`);
await $__gscc.util.sleep(queueTime);
}

const response = await this.retrieveCitationData(item);
$__gscc.debugger.info(`ResponseText: ${response.responseText}`);
await this.processCitationResponse(
response.status,
response.responseText,
Expand Down Expand Up @@ -623,15 +594,16 @@ $__gscc.app = {
);

let titleSearchString;
let rawTitle = item.getField('title').replace(/<[^>]+>/g, '');
if (useSearchTitleFuzzyMatch) {
$__gscc.debugger.info(
`Search Param: Using Fuzzy Title Match per Preferences`,
);
titleSearchString = `${item.getField('title')}`;
titleSearchString = `${rawTitle}`;
} else {
// this is a dead match; kinda risky for hand-entered data but match is
// good on Zotero grabs
titleSearchString = `"${item.getField('title')}"`;
titleSearchString = `"${rawTitle}"`;
}

let paramAuthors = '';
Expand Down Expand Up @@ -661,7 +633,13 @@ $__gscc.app = {
}
}

const targetUrl = `${this.__apiEndpoint}scholar?hl=en&q=${titleSearchString}&as_epq=&as_occt=title&num=1${paramAuthors}${paramYearRange}`;
const scholarurl = Zotero.Prefs.get(
'extensions.zotero.gscc.mirrorurl',
$__gscc.app.__preferenceDefaults.mirrorurl,
);
const targetUrl = `${scholarurl}scholar?hl=zh&q=${titleSearchString}&as_epq=&as_occt=title&num=1${paramAuthors}${paramYearRange}`;


$__gscc.debugger.info(`Search Endpoint Ready: ${targetUrl}`);

return encodeURI(targetUrl);
Expand Down
5 changes: 1 addition & 4 deletions src/locale/en-US/gscc-prefs.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
preferences-gscc-enable-random-wait-timing = Set Google Scholar Request Random Wait Intervals
preferences-gscc-random-wait-timing-explain = To attempt to not trigger the IP shadow ban that Google Scholar implements, GSCC uses a random interval per HTTP request. You can change the window by revising the milliseconds below.
preferences-gscc-randomWaitMinMs = Minimum Request Wait (milliseconds)
preferences-gscc-randomWaitMaxMs = Maximum Request Wait (milliseconds)
preferences-gscc-mirror-title=Mirror URL
preferences-gscc-search-params = Custom Search Parameters
preferences-gscc-search-params-explain = Depending on the types of papers you import, sometimes finding matches can be hard. The latest v4.1 of GSCC allows changing the search behavior through flags to help when you need it. In most cases, you shouldn't need the flags below, but if you're having issues, different combinations in different global regions can sometimes help.
preferences-gscc-useSearchTitleFuzzyMatch= Use Fuzzy Title Match
Expand Down
5 changes: 1 addition & 4 deletions src/locale/es-ES/gscc-prefs.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
preferences-gscc-enable-random-wait-timing = Establecer intervalos de espera aleatorios para las solicitudes de Google Scholar
preferences-gscc-random-wait-timing-explain = Para intentar no activar la prohibición de IP que implementa Google Scholar, GSCC utiliza un intervalo aleatorio por cada solicitud HTTP. Puede cambiar la ventana modificando los milisegundos a continuación.
preferences-gscc-randomWaitMinMs = Espera mínima de solicitud (milisegundos)
preferences-gscc-randomWaitMaxMs = Espera máxima de solicitud (milisegundos)
preferences-gscc-mirror-title=Espejo URL
preferences-gscc-search-params = Parámetros de búsqueda personalizados
preferences-gscc-search-params-explain = Dependiendo de los tipos de documentos que importe, a veces puede ser difícil encontrar coincidencias. La última versión v4.1 de GSCC permite cambiar el comportamiento de búsqueda mediante opciones para ayudarle cuando lo necesite. En la mayoría de los casos, no necesitará las opciones a continuación, pero si tiene problemas, diferentes combinaciones en distintas regiones globales pueden ayudar.
preferences-gscc-useSearchTitleFuzzyMatch = Usar coincidencia de título difusa
Expand Down
5 changes: 1 addition & 4 deletions src/locale/fr-FR/gscc-prefs.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
preferences-gscc-enable-random-wait-timing = Définir des intervalles d'attente aléatoires pour les requêtes Google Scholar
preferences-gscc-random-wait-timing-explain = Pour tenter de ne pas déclencher l'interdiction IP imposée par Google Scholar, GSCC utilise un intervalle aléatoire pour chaque requête HTTP. Vous pouvez modifier la fenêtre en révisant les millisecondes ci-dessous.
preferences-gscc-randomWaitMinMs = Temps d'attente minimum (millisecondes)
preferences-gscc-randomWaitMaxMs = Temps d'attente maximum (millisecondes)
preferences-gscc-mirror-title=Miroir URL
preferences-gscc-search-params = Paramètres de recherche personnalisés
preferences-gscc-search-params-explain = En fonction des types de documents que vous importez, il peut parfois être difficile de trouver des correspondances. La dernière version v4.1 de GSCC permet de modifier le comportement de recherche via des options pour vous aider en cas de besoin. Dans la plupart des cas, vous n'aurez pas besoin de ces options, mais si vous rencontrez des problèmes, différentes combinaisons dans différentes régions peuvent parfois aider.
preferences-gscc-useSearchTitleFuzzyMatch = Utiliser la correspondance floue des titres
Expand Down
5 changes: 1 addition & 4 deletions src/locale/ja-JP/gscc-prefs.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
preferences-gscc-enable-random-wait-timing = Google Scholar リクエストのランダム待機間隔を設定
preferences-gscc-random-wait-timing-explain = Google Scholar が実施する IP ブロックを回避するため、GSCC は各 HTTP リクエストにランダムな間隔を使用します。以下のミリ秒単位の設定を変更してウィンドウを調整できます。
preferences-gscc-randomWaitMinMs = 最小リクエスト待機時間(ミリ秒)
preferences-gscc-randomWaitMaxMs = 最大リクエスト待機時間(ミリ秒)
preferences-gscc-mirror-title=ミラーURL
preferences-gscc-search-params = カスタム検索パラメータ
preferences-gscc-search-params-explain = インポートする論文の種類によっては、マッチを見つけるのが難しい場合があります。最新の GSCC v4.1 では、必要に応じてフラグを使用して検索動作を変更することができます。ほとんどの場合、以下のフラグは必要ありませんが、問題が発生した場合、異なる地域で異なる組み合わせが役立つことがあります。
preferences-gscc-useSearchTitleFuzzyMatch = タイトルのファジーマッチを使用
Expand Down
5 changes: 1 addition & 4 deletions src/locale/zh-CH/gscc-prefs.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
preferences-gscc-enable-random-wait-timing = 设置 Google 学术请求随机等待时间间隔
preferences-gscc-random-wait-timing-explain = 为了尝试避免触发 Google 学术实施的 IP 阴影禁令,GSCC 对每个 HTTP 请求使用随机间隔。您可以通过修改下面的毫秒数来更改窗口。
preferences-gscc-randomWaitMinMs = 最小请求等待时间(毫秒)
preferences-gscc-randomWaitMaxMs = 最大请求等待时间(毫秒)
preferences-gscc-mirror-title=镜像网址
preferences-gscc-search-params = 自定义搜索参数
preferences-gscc-search-params-explain = 根据您导入的论文类型,有时找到匹配项会很困难。最新的 GSCC v4.1 允许通过标志改变搜索行为,以便在需要时提供帮助。在大多数情况下,您不需要使用以下标志,但如果遇到问题,不同全球区域的不同组合有时会有所帮助。
preferences-gscc-useSearchTitleFuzzyMatch = 使用模糊标题匹配
Expand Down
6 changes: 3 additions & 3 deletions src/locale/zh-CH/gscc.ftl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
gscc-menuitem =
.label = 更新 Google 学术引用次数
gscc-column-name = 引用次数
.label = 更新 Google 学术被引次数
gscc-column-name = 被引次数
gscc-update-all =
.label = 更新所有 Google 学术引用次数
.label = 更新所有 Google 学术被引次数
gscc-recapatcha-alert = 请在现在打开的页面上输入验证码,然后重试更新引用,或者如果没有出现验证码,请等待一段时间以解除 Google 的限制。
gscc-citedByPrefix = 引用自
gscc-lackPermissions = 您没有权限编辑此库。
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Google Scholar Citation Count",
"version": "4.1.1",
"version": "5.0.0",
"description": "Zotero plugin for fetching numbers of citations from Google Scholar.",
"homepage_url": "https://github.com/justinribeiro/zotero-google-scholar-citation-count",
"author": "Justin Ribeiro",
Expand Down
6 changes: 2 additions & 4 deletions src/prefs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pref('extensions.zotero.gscc.useRandomWait', true);
pref('extensions.zotero.gscc.randomWaitMinMs', 1000);
pref('extensions.zotero.gscc.randomWaitMaxMs', 5000);
pref('extensions.zotero.gscc.mirrorurl', 'https://scholar.google.com/');
pref('extensions.zotero.gscc.useSearchTitleFuzzyMatch', false);
pref('extensions.zotero.gscc.useSearchAuthorsMatch', true);
pref('extensions.zotero.gscc.useDateRangeMatch', false);
pref('extensions.zotero.gscc.useSearchAuthorsMatch', true);
Loading