forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add translator for Stack Exchange sites
Fixes zotero#2805
- Loading branch information
1 parent
07d6d82
commit 8ea05e0
Showing
1 changed file
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
{ | ||
"translatorID": "26ecfee6-535d-4044-89a8-fbda31883642", | ||
"label": "Stack Exchange", | ||
"creator": "Abe Jellinek", | ||
"target": "^https://([^/]+\\.)?(((stack(overflow|exchange)|serverfault|askubuntu)\\.com)|mathoverflow\\.net)/", | ||
"minVersion": "5.0", | ||
"maxVersion": "", | ||
"priority": 100, | ||
"inRepository": true, | ||
"translatorType": 4, | ||
"browserSupport": "gcsibv", | ||
"lastUpdated": "2022-03-28 19:01:35" | ||
} | ||
|
||
/* | ||
***** BEGIN LICENSE BLOCK ***** | ||
Copyright © 2022 Abe Jellinek | ||
This file is part of Zotero. | ||
Zotero is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Zotero is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with Zotero. If not, see <http://www.gnu.org/licenses/>. | ||
***** END LICENSE BLOCK ***** | ||
*/ | ||
|
||
|
||
async function detectWeb(doc, _url) { | ||
if (doc.querySelector('div[itemprop="mainEntity"][itemtype="https://schema.org/Question"]') | ||
|| doc.querySelector('#content > [itemscope]')) { | ||
return 'multiple'; | ||
} | ||
return false; | ||
} | ||
|
||
function getSearchResults(doc, checkOnly) { | ||
var items = {}; | ||
var found = false; | ||
var rows = doc.querySelectorAll('#question, .answer'); | ||
for (let row of rows) { | ||
let id = row.id; | ||
let summary = ZU.trimInternal(text(row, '[itemprop="text"] p')); | ||
if (!id || !summary) continue; | ||
if (checkOnly) return true; | ||
if (id == 'question') { | ||
summary = 'Q: ' + summary; | ||
} | ||
else { | ||
summary = 'A: ' + summary; | ||
} | ||
found = true; | ||
items[id] = summary; | ||
} | ||
return found ? items : false; | ||
} | ||
|
||
async function doWeb(doc, _url) { | ||
Zotero.selectItems(getSearchResults(doc, false), function (items) { | ||
if (items) { | ||
Object.keys(items).forEach(id => scrape(doc, id)); | ||
} | ||
}); | ||
} | ||
|
||
function scrape(doc, id) { | ||
let el = doc.getElementById(id); | ||
let item = new Zotero.Item('forumPost'); | ||
item.forumTitle = attr(doc, 'meta[property="og:site_name"]', 'content'); | ||
item.date = ZU.strToISO(attr(el, 'time[itemprop="dateCreated"]', 'datetime') | ||
|| attr(el, '.relativetime', 'title')); | ||
// .href converts the URL to absolute | ||
item.url = el.querySelector('a[itemprop="url"]').href; | ||
item.creators.push(ZU.cleanAuthor(text(el, '[itemprop="author"] a'), 'author')); | ||
item.attachments.push({ | ||
title: 'Snapshot', | ||
document: doc | ||
}); | ||
|
||
if (id == 'question') { | ||
item.title = attr(doc, 'meta[itemprop="name"]', 'content'); | ||
item.postType = 'Forum post'; | ||
} | ||
else { | ||
item.title = `Answer to "${attr(doc, 'meta[itemprop="name"]', 'content')}"`; | ||
} | ||
|
||
item.complete(); | ||
} | ||
|
||
/** BEGIN TEST CASES **/ | ||
var testCases = [ | ||
{ | ||
"type": "web", | ||
"url": "https://mathoverflow.net/questions/318890/steepest-descent-integration-in-several-dimensions/418958", | ||
"items": "multiple" | ||
}, | ||
{ | ||
"type": "web", | ||
"url": "https://stats.stackexchange.com/questions/210352/do-cohens-d-and-hedges-g-apply-to-the-welch-t-test", | ||
"items": "multiple" | ||
}, | ||
{ | ||
"type": "web", | ||
"url": "https://es.stackoverflow.com/questions/524520/eliminar-espacio-de-fields-vac%c3%ados-en-reporte-con-jasperreport", | ||
"items": "multiple" | ||
}, | ||
{ | ||
"type": "web", | ||
"url": "https://stackoverflow.com/questions/178325/how-do-i-check-if-an-element-is-hidden-in-jquery", | ||
"items": "multiple" | ||
} | ||
] | ||
/** END TEST CASES **/ |