Skip to content
Merged
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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ google('node.js best practices', function (err, next, links){
```

You can also specify the TLD of the Google search page and the language.
If you change the language you must translate the next page results text to detect the corresponding link.

```js
var google = require('google')

google.lang = 'de'
google.tld = 'de'
google.nextText = 'Weiter'

google('node.js best practices', function (err, next, links){
Expand Down
6 changes: 5 additions & 1 deletion lib/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var nextSel = 'td.b a span'

var URL = 'http://www.google.%s/search?hl=%s&q=%s&start=%s&sa=N&num=%s&ie=UTF-8&oe=UTF-8'

var nextTextErrorMsg = 'Translate `google.nextText` option to selected language to detect next results link.'

function google (query, callback) {
igoogle(query, 0, callback)
}
Expand All @@ -18,9 +20,11 @@ google.resultsPerPage = 10
google.tld = 'com'
google.lang = 'en'
google.requestOptions = {}
google.nextText = 'Next'

var igoogle = function (query, start, callback) {
if (google.resultsPerPage > 100) google.resultsPerPage = 100 // Google won't allow greater than 100 anyway
if (google.lang !== 'en' && google.nextText === 'Next') callback(new Error(nextTextErrorMsg, null, null))

var newUrl = util.format(URL, google.tld, google.lang, querystring.escape(query), start, google.resultsPerPage)
var requestOptions = {
Expand Down Expand Up @@ -60,7 +64,7 @@ var igoogle = function (query, start, callback) {
})

var nextFunc = null
if ($(nextSel).last().text() === 'Next') {
if ($(nextSel).last().text() === google.nextText) {
nextFunc = function () {
igoogle(query, start + google.resultsPerPage, callback)
}
Expand Down
32 changes: 32 additions & 0 deletions test/google.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,36 @@ describe('+ google()', function () {

})
})

describe('when nextText and lang are set', function () {
it('should return next page search results', function (done) {
var nextCounter = 0
var allLinks = []
var query = 'Microsoft'

var finished = function () {
assert(allLinks.length > 25)
done()
}

google.resultsPerPage = 25
google.lang = 'it'
google.nextText = 'Avanti'
google(query, function (err, next, links) {
assert.ifError(err)
allLinks = allLinks.concat(links)
if (nextCounter < 2) {
if (next) {
nextCounter += 1
next()
} else {
finished()
}
} else {
finished()
}
})

})
})
})