Skip to content

Commit 336ff3f

Browse files
committed
fix goolge
1 parent 883522d commit 336ff3f

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

src/js/reverseImageSearch.js

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -176,53 +176,20 @@ export const reverseImageSearch = {
176176
const tagA = singleItem.getElementsByTagName("a");
177177
singleResult.sourceUrl = tagA[0].getAttribute("href");
178178
singleResult.title = tagA[0].innerText;
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;
179+
//new method to dig the image Source
180+
for(let i = 2; i < tagA.length;i++){
181+
if(tagA[i]){
182+
let link = tagA[i].getAttribute("href");
183+
//console.log(tagA[i]);
184+
link = parseGoogleImageLink(link);
185+
if(link) {
186+
console.log(link);
187+
singleResult.imageUrl = link;
188+
singleResult.thumbUrl = link;
189+
break;
190+
}
191+
}
191192
}
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;
223-
//process description
224-
//class st span contain N child, first child is size info
225-
//behind children are description,conbine them
226193
const tagSpan = singleItem.getElementsByClassName("st")[0].childNodes;
227194
let description = "";
228195
for (let i = 0; i < tagSpan.length; i++) {

src/utils/imageUtils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ export const checkUrlOrBase64 = function(item){
5151
}
5252
}
5353
export const parseGoogleImageLink = function(link){
54-
let pareseString = /(http|https).*(\.jpg|\.jpeg|\.png|\.gif|\.svg)/g;
55-
return link.match(pareseString);
54+
let parseReg = /(http|https).*(\.jpg&imgrefurl|\.jpeg&imgrefurl|\.png&imgrefurl|\.gif&imgrefurl|\.svg&imgrefurl|&imgrefurl)/g;
55+
let parseResult = link.match(parseReg);
56+
if(parseResult){
57+
let resultLink = parseResult[0];
58+
//console.log(resultLink.substring(0,resultLink.length-10));
59+
return resultLink.substring(0,resultLink.length-10);
60+
}else{
61+
return null;
62+
}
5663
}
5764
export const sortImageByRelevance = function(data){
5865
data.sort(function(a,b){

0 commit comments

Comments
 (0)