我正在写一个Bookmarklet来改变google主页的行为。我喜欢视觉搜索页面预览功能。 Coolest.thing.ever。 但是我真的很想看看它是如何作为实时搜索使用的,而不是将鼠标悬停在链接上才能看到它。为什么不是我用JavaScript点击添加到DOM的链接?
所以有几个步骤 - 我已经部分功能工作。
这里是JS代码。
javascript: (function() {
c = document.getElementById('ires');
nli = document.createElement('div');
cell = 0;
for (var Obj in google.vs.ha) {
na = document.createElement('a');
na.href = Obj;
na.style.cssFloat = 'left';
na.style.styleFloat = 'left';
nd = document.createElement('div');
cldiv = document.createElement('div');
cldiv.style.clear = 'both';
nd.style.width = google.vs.ha[Obj].data.dim[0] + 'px';
nd.style.height = google.vs.ha[Obj].data.dim[1] + 'px';
nd.style.margin = '5px';
nd.style.padding = '5px';
nd.style.border = '1px solid #999999';
if (google.vs.ha[Obj].data.tbts.length) {
nilDiv = document.createElement('div');
for (i = 0; i < google.vs.ha[Obj].data.tbts.length; i++) {
box = google.vs.ha[Obj].data.tbts[i].box;
newbox = document.createElement('div');
newbox.className = 'vsb vsbb';
newbox.style.position = 'relative';
newbox.style.top = (box.t) + 'px';
newbox.style.left = box.l + 'px';
newbox.style.height = box.h + 'px';
newbox.style.width = box.w + 'px';
nilDiv.appendChild(newbox);
newtext = document.createElement('div');
newtext.className = 'vsb vstb';
newtext.innerHTML = google.vs.ha[Obj].data.tbts[i].txt;
newtext.style.top = (box.t) + 'px';
newtext.style.position = 'relative';
nilDiv.appendChild(newtext);
}
nilDiv.style.height = '0px';
nd.appendChild(nilDiv);
}
for (i = 0; i < google.vs.ha[Obj].data.ssegs.length; i++) {
ni = document.createElement('img');
ni.src += google.vs.ha[Obj].data.ssegs[i];
ni.className += ' vsi';
nd.appendChild(ni);
}
na.appendChild(nd);
nli.appendChild(na);
};
c.insertBefore(nli, c.firstChild);
})();
它工作在目前的方法是
- 把上面的代码中书签
- 搜索对谷歌
- 东西点击放大镜
- 点击书签
目前,出于某种原因 - 添加到的链接除非图像预览中有文本框,否则该页面不可点击。就我对DOM的理解而言,<a>
标签的全部内容应该是可点击的。
任何人都知道问题是什么?
我想弄清楚的其他问题是如何在不需要用户点击的情况下自动查询图像。
一旦完成,我会尝试将整个事情变成一个事件侦听器,它可以在搜索窗口中自动查询和显示图像。
那会是多酷?! :)
我似乎无法得到书签在这里“编译”在堆栈,但我有一个你可以拖动到您的工具栏在这里:http://chesser.ca/2010/11/google-visual-image-search-hack-marklet。
或在href应该是这样的
<a href="javascript:(function(){c=document.getElementById('ires');nli=document.createElement('div');cell=0;for(var Obj in google.vs.ha){na=document.createElement('a');na.href=Obj;na.style.cssFloat='left';na.style.styleFloat='left';nd=document.createElement('div');cldiv=document.createElement('div');cldiv.style.clear='both';nd.style.width=google.vs.ha[Obj].data.dim[0]+'px';nd.style.height=google.vs.ha[Obj].data.dim[1]+'px';nd.style.margin='5px';nd.style.padding='5px';nd.style.border='1px solid #999999';if(google.vs.ha[Obj].data.tbts.length){nilDiv=document.createElement('div');for(i=0;i<google.vs.ha[Obj].data.tbts.length;i++){box=google.vs.ha[Obj].data.tbts[i].box;newbox=document.createElement('div');newbox.className='vsb vsbb';newbox.style.position='relative';newbox.style.top=(box.t)+'px';newbox.style.left=box.l+'px';newbox.style.height=box.h+'px';newbox.style.width=box.w+'px';nilDiv.appendChild(newbox);newtext=document.createElement('div');newtext.className='vsb vstb';newtext.innerHTML=google.vs.ha[Obj].data.tbts[i].txt;newtext.style.top=(box.t)+'px';newtext.style.position='relative';nilDiv.appendChild(newtext);}nilDiv.style.height='0px';nd.appendChild(nilDiv);}for(i=0;i<google.vs.ha[Obj].data.ssegs.length;i++){ni=document.createElement('img');ni.src+=google.vs.ha[Obj].data.ssegs[i];ni.className+=' vsi';nd.appendChild(ni);}na.appendChild(nd);nli.appendChild(na);};c.insertBefore(nli,c.firstChild);})();">bookmarklet</a>
我现在就试试!非常感谢。 – 2010-11-21 19:49:21
奇怪的是,如果我将图像移动到A元素中唯一的图像,图像是可点击的....但只有当我不将styleFloat应用于包含它们的DIV时。 – 2010-11-21 20:06:46