Skip to content

Commit 7ab8784

Browse files
committed
fix google
1 parent 1cfdb95 commit 7ab8784

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

src/js/reverseImageSearch.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import ajax from 'SRC/utils/ajax.js';
77
import ENGINE_DONE from 'SRC/constant/constants.js';
88
import { ENGINE_WEIGHTS } from '../constant/constants';
9+
import {parseGoogleImageLink} from 'SRC/utils/imageUtils';
910
const HTML = new DOMParser();
1011
// Data Format
1112
export const reverseImageSearch = {
@@ -168,32 +169,57 @@ export const reverseImageSearch = {
168169
weight: ENGINE_WEIGHTS.google - i + Math.random(),
169170
}
170171
const singleItem = list[i];
172+
//console.log(singleItem);
171173
//process title,imageUrl,sourceUrl and thumbUrl
172174
//first <a> contain title and imageUrl
173175
//second <a> contain sourceUrl and thumbUrl
174176
const tagA = singleItem.getElementsByTagName("a");
175177
singleResult.sourceUrl = tagA[0].getAttribute("href");
176178
singleResult.title = tagA[0].innerText;
177-
let link = tagA[1].getAttribute("href");
178-
singleResult.imageUrl = link.substring(link.indexOf("=") + 1, link.indexOf("&imgre"));
179-
if (!singleResult.imageUrl) {
180-
let tempImgLink = tagA[3].getAttribute("href");
181-
console.log('----------------');
182-
console.log(tempImgLink);
183-
tempImgLink = tempImgLink.substr(tempImgLink.indexOf("imgurl=") + "imgurl=".length);
184-
tempImgLink = tempImgLink.substr(0, tempImgLink.indexOf("&"));
185-
console.log(tempImgLink);
186-
if (tempImgLink.match(/\:s$/)) {
187-
tempImgLink = tempImgLink.substr(0, tempImgLink.length - 2);
188-
console.log(tempImgLink);
189-
}
190-
else if(tempImgLink.indexOf("%3") !== -1) {
191-
tempImgLink = tempImgLink.substr(0, tempImgLink.indexOf("%3"));
192-
console.log(tempImgLink);
193-
}
194-
singleResult.imageUrl = tempImgLink;
195-
}
196-
singleResult.thumbUrl = singleResult.imageUrl;
179+
// console.log(tagA.length);
180+
// console.log(tagA[0]);
181+
// console.log(tagA[1]);
182+
// console.log(tagA[2]);
183+
// console.log(tagA[3]);
184+
// console.log(tagA[4]);
185+
// console.log("----------");
186+
if(tagA[4]){
187+
let link = tagA[4].getAttribute("href");
188+
link = parseGoogleImageLink(link);
189+
singleResult.imageUrl = link;
190+
singleResult.thumbUrl = link;
191+
}
192+
//console.log({a:tagA[1],b:tagA[2],c:tagA[3],d:tagA[0]});
193+
//console.log(tagA[1]);
194+
//console.log(tagA[2]);
195+
//console.log(singleResult.sourceUrl );
196+
//console.log(tagA.length);
197+
// let test = tagA[2].getAttribute("href");
198+
// console.log(test);
199+
// console.log(parseGoogleImageLink(test));
200+
//let link = tagA[1].getAttribute("href");
201+
202+
//singleResult.imageUrl = link.substring(link.indexOf("=") + 1, link.indexOf("&imgre"));
203+
//console.log(singleResult.imageUrl);
204+
205+
// // if (!singleResult.imageUrl) {
206+
// // let tempImgLink = tagA[3].getAttribute("href");
207+
// // //console.log('----------------');
208+
// // //console.log(tempImgLink);
209+
// // tempImgLink = tempImgLink.substr(tempImgLink.indexOf("imgurl=") + "imgurl=".length);
210+
// // tempImgLink = tempImgLink.substr(0, tempImgLink.indexOf("&"));
211+
// // //console.log(tempImgLink);
212+
// // if (tempImgLink.match(/\:s$/)) {
213+
// // tempImgLink = tempImgLink.substr(0, tempImgLink.length - 2);
214+
// // console.log(tempImgLink);
215+
// // }
216+
// // else if(tempImgLink.indexOf("%3") !== -1) {
217+
// // tempImgLink = tempImgLink.substr(0, tempImgLink.indexOf("%3"));
218+
// // //console.log(tempImgLink);
219+
// // }
220+
// // singleResult.imageUrl = tempImgLink;
221+
// // }
222+
// singleResult.thumbUrl = singleResult.imageUrl;
197223
//process description
198224
//class st span contain N child, first child is size info
199225
//behind children are description,conbine them

src/utils/imageUtils.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ export const checkUrlOrBase64 = function(item){
5050
return "no"
5151
}
5252
}
53-
53+
export const parseGoogleImageLink = function(link){
54+
let pareseString = /(http|https).*(\.jpg|\.jpeg|\.png|\.gif|\.svg)/g;
55+
return link.match(pareseString);
56+
}
5457
export const sortImageByRelevance = function(data){
5558
data.sort(function(a,b){
5659
if(a.weight && b.weight){
5760
return b.weight - a.weight;
58-
}
61+
}
5962
})
60-
}
63+
}

0 commit comments

Comments
 (0)