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

New plug-in for: StartPage search engine #829

Merged
merged 1 commit into from
Dec 5, 2021
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
4 changes: 4 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,10 @@
{
"js": ["plugins/allegro.js"],
"matches": ["*://*.allegro.pl/*"]
},
{
"js": ["plugins/startpage.js"],
"matches": ["*://*.startpage.com/*"]
}
]
}
182 changes: 182 additions & 0 deletions plugins/startpage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
var hoverZoomPlugins = hoverZoomPlugins || [];
hoverZoomPlugins.push({
name:'startpage',
version:'0.1',
prepareImgLinks:function (callback) {

var name = this.name;
var res = [];
var jsonToken1 = 'UIStartpage.App,';
var jsonToken2 = '}),';
var spJson = undefined;
var spData = extractJsonFromDoc();

// links to fullsize imgs are stored in HTML document (JSON)
function extractJsonFromDoc() {

let innerHTML = document.documentElement.innerHTML;
let index1 = innerHTML.indexOf(jsonToken1);
if (index1 == -1) return undefined;
let index2 = innerHTML.indexOf(jsonToken2, index1);
spJson = innerHTML.substring(index1 + jsonToken1.length, index2 + 1);
try {
let sp = JSON.parse(spJson);
return sp;
} catch { return undefined }

return undefined;
}

// Find key(s) in JSON object and return corresponding value(s) and path(s)
// If key not found then return []
// Search is NOT case-sensitive
// https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0
function getKeysInJsonObject(jsonObj, searchKey, isRegex, maxDeepLevel, currDeepLevel) {

var bShowInfo = false;

maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100;
currDeepLevel = currDeepLevel ? currDeepLevel : 1 ;
isRegex = isRegex ? isRegex : false;

// check RegEx validity if needed
var re;
if (isRegex) {
try {
re = new RegExp(searchKey);
} catch (e) {
cLog(e);
return [];
}
}

if ( currDeepLevel > maxDeepLevel ) {
return [];
} else {

var keys = [];

for ( var curr in jsonObj ) {
var currElem = jsonObj[curr];

if ( currDeepLevel == 1 && bShowInfo ) { cLog("getKeysInJsonObject : Looking property \"" + curr + "\" ") }

if ( isRegex ? re.test(curr) : curr.toLowerCase() === searchKey.toLowerCase() ){
var r = {};
r.key = curr;
r.value = currElem;
r.path = '["' + curr + '"]';
r.depth = currDeepLevel;
keys.push( r );
}

if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof"
// search again :D
var deepKeys = getKeysInJsonObject( currElem, searchKey, isRegex, maxDeepLevel, currDeepLevel + 1 );

for ( var e = 0; e < deepKeys.length; e++ ) {
// update path backwards
deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path;
keys.push( deepKeys[e] );
}
}
}
return keys;
}
}

// Find value(s) in JSON object and return corresponding key(s) and path(s)
// If value not found then return []
// Search is NOT case-sensitive
// ref: https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0
function getValuesInJsonObject(jsonObj, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel) {

var bShowInfo = false;

maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100;
currDeepLevel = currDeepLevel ? currDeepLevel : 1 ;
isRegex = isRegex ? isRegex : false;

// check RegEx validity if needed
var re;
if ( isRegex ) {
try {
re = new RegExp(searchValue);
} catch (e) {
cLog(e);
return [];
}
} else {
searchValue = searchValue.toString().toLowerCase();
}

if ( currDeepLevel > maxDeepLevel ) {
return [];
} else {

var keys = [];

for ( var curr in jsonObj ) {
var currElem = jsonObj[curr];

if ( currDeepLevel == 1 && bShowInfo ) { cLog("getValuesInJsonObject : Looking property \"" + curr + "\" ") }

if ( typeof currElem == "undefined" ) continue;

if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof"
// search again :D
var deepKeys = getValuesInJsonObject( currElem, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel + 1 );
for ( var e = 0; e < deepKeys.length; e++ ) {
// update path backwards
deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path;
keys.push( deepKeys[e] );
}
} else {

if ( isRegex ? re.test(currElem) : ( isPartialMatch ? currElem.toString().toLowerCase().indexOf(searchValue) != -1 : currElem.toString().toLowerCase() === searchValue ) ){

var r = {};
r.key = curr;
r.value = currElem;
r.path = '["' + curr + '"]';
r.depth = currDeepLevel;
keys.push( r );
if (isFirstMatchOnly) return keys;
}
}
}
return keys;
}
}

// Return JSON object corresponding to path, without using the Evil eval
// path syntax: [key1][key2][key3]...
function getJsonObjectFromPath(path) {
return new Function('return ' + spJson + path)();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrosPoulet This is unsafe code which is banned by Mozilla, please take a look at my fix in 98a6d97

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@extesy I don't see what's wrong but i trust Mozilla, they are certainly more experts that i am 😄
I'll apply your changes to StartPage plug-in & move following functions to hoverzoom.js:

  • getJsonObjectFromPath
  • getKeysInJsonObject
  • getValuesInJsonObject

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it makes sense to move these functions to a common code since they are used across multiple plugins.

}

//$('link[href*="proxy-image"]:not(.hoverZoomFetched)').addClass('hoverZoomFetched').each(function () {
$('img[src*="proxy-image"]:not(.hoverZoomFetched)').addClass('hoverZoomFetched').each(function () {
let link = $(this);
let src = link.attr('src');

// search for thumbnail url among sp data
if (spJson.indexOf(src) == -1) return;

let values = getValuesInJsonObject(spData, src, false, true, true); // look for a partial match & stop after 1st match
if (values.length == 0) return;
let o = getJsonObjectFromPath(values[0].path.substring(0, values[0].path.lastIndexOf('[')));
// extract fullsize url from Object
let fullsizeUrl = o.clickUrl || o.anonImageViewUrl;
fullsizeUrl = fullsizeUrl.replace(/.*piurl=(.*)&sp=.*/, '$1');
fullsizeUrl = decodeURIComponent(fullsizeUrl);
if (link.data().hoverZoomSrc == undefined) { link.data().hoverZoomSrc = [] }
if (link.data().hoverZoomSrc.indexOf(fullsizeUrl) == -1) {
link.data().hoverZoomSrc.unshift(fullsizeUrl);
res.push(link);
}
});

callback($(res), name);
}
});