Skip to content

Commit

Permalink
Add support for Baidu AI-generated results
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Oct 3, 2024
1 parent bddaf47 commit 5e67213
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions modules/baidu.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ zoekplaatje.register_module(
description: 'span[class*=content-right], .c-span-last'
};

/**
* Get an HTML element's property, safely
*
* Trying to get e.g. the innerText of a non-existing element crashes
* the script, this makes it return an empty string instead.
*
* @param item
* @param prop
* @param default_value
* @returns {*|string}
*/
function safe_prop(item, prop, default_value='') {
if(item && prop.indexOf('attr:') === 0 && item.hasAttribute(prop.split('attr:')[1])) {
return item.getAttribute(prop.split('attr:')[1]);
} else if(item && prop in item) {
return item[prop].trim();
} else {
return default_value;
}
}

// check if file contains search results...
// if so, create a DOM with the results we can query via selectors
if (path.indexOf('wd=') > 0) {
Expand Down Expand Up @@ -178,6 +199,13 @@ zoekplaatje.register_module(
title: item.querySelector('div[class*=rs-label]').innerText,
description: Array.from(item.querySelectorAll('td')).map(t => t.innerText).join(', ')
}
} else if(item.querySelector("*[class*='generative-content-container']")) {
parsed_item = {
...parsed_item,
type: 'ai-generated-answer-widget',
title: '',
description: safe_prop(item.querySelector('.cosd-searching-step-content'), 'innerText').trim(),
}
} else {
console.log(item);
}
Expand Down

0 comments on commit 5e67213

Please sign in to comment.