小书签如何避免弹出式窗口拦截器 [英] How a bookmarklet can avoid the popup blocker

查看:166
本文介绍了小书签如何避免弹出式窗口拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  javascript:(function()()函数用来在Google翻译器中快速翻译所选文本。 {
var text = encodeURI(document.getSelection());
if(!text.length){
text = prompt('Texto')
}
var url ='http://translate.google.com/translate_t?hl=&ie=UTF-8&text='+ text +'& sl = es& tl = pt#';
window .open(url,'trans','left = 20,top = 20,width = 1000,height = 500,toolbar = 0,location = 0,resizable = 1');
})();

但是,Firefox弹出窗口阻止程序不允许打开新窗口。我可以为每个使用弹出窗口的网站添加例外情况,但是这可能会让人很烦恼...



我以为bookmarklets可以打开弹出窗口 - 其实很多他们这样做吧?我究竟做错了什么?还是不可能做到这一点?

解决方案

还有另一种解决弹出窗口拦截器的方法,首先包括一个链接覆盖在页面上,然后让用户点击它来生成弹出窗口。小书签javascript然后可以存储在一个单独的文件。这是Pinterest的小书签管理如何做到这一点。首先,他们从页面中选择图像并将其直接覆盖在页面上。然后,当用户点击选择其中一个照片弹出窗口。因为这个动作是由用户发起的,所以弹出窗口起作用。



将这个代码放在一个名为bookmarklet.js的文件中

p>

  var popupProperties ='width = 600,height = 400,toolbar = 0,location = 0,resizable = 1'; 
var newA = document.createElement(a);
var url ='http://www.stackoverflow.com';
newA.setAttribute(href,javascript:window.open(url,'Hi',popupProperties););
newA.setAttribute(style,position:fixed; z-index:9999999; top:0; left:0; width:100px; height:100px; color:#000; background:#fff; display :块;);
var newT = document.createTextNode(Open this);
newA.appendChild(newT);
document.body.appendChild(newA);

然后您的书签链接可以是这样的:

  javascript:var jsCode = document.createElement('script'); jsCode.setAttribute('src','http://localhost/bookmarklet.js?r ='+的Math.random()* 99999999); document.body.appendChild(jsCode); 

或者,您需要在实际的书签链接中包含弹出窗口。这反过来将意味着做任何改变的唯一方法是为用户重新安装书签。编辑:除了上面的方法,我晚些时候发现使用easyXDM还有另一种解决方法。它可以帮助您解决同源策略 http://easyxdm.net/wp/



使用此功能,您可以使用iframe作为书签,甚至可以在iframe中使用关闭链接,以便从父页面中移除iframe。


I wrote a bookmarklet for quickly translating selected text using Google Translator in a popup window:

javascript:(function(){
    var text = encodeURI(document.getSelection());
    if (!text.length) {
        text = prompt('Texto')
    }
    var url = 'http://translate.google.com/translate_t?hl=&ie=UTF-8&text=' + text + ' &sl=es&tl=pt#';
    window.open(url,'trans','left=20,top=20,width=1000,height=500,toolbar=0,location=0,resizable=1');
})();

However, the Firefox popup blocker does not allow the new window to be opened. I can add exceptions for every site where I use the popup, but it can be pretty annoying...

I thought bookmarklets could open popup windows - actually, a lot of them do it, right? What am I doing wrong? Or is it not possible to do it?

解决方案

There is another way of working around the popup blocker by first including a link overlaid on the page and then allowing the user to click that to generate the popup. The bookmarklet javascript can then be stored in a separate file. This is how Pinterest's bookmarklet manages to do it. First they select images from the page and overlay it directly on the page. Then when the user clicks to select one of the photos the popup appears. Because this action was initiated by the user, the popup works.

Here's some code you can use to test:

Place this in a file named bookmarklet.js

var popupProperties='width=600,height=400,toolbar=0,location=0,resizable=1';
var newA = document.createElement("a");
var url = 'http://www.stackoverflow.com';
newA.setAttribute("href","javascript:window.open(url,'Hi',popupProperties);");
newA.setAttribute("style","position:fixed;z-index:9999999;top:0;left:0;width:100px;height:100px;color:#000;background:#fff;display:block;");
var newT = document.createTextNode("Open this");
newA.appendChild(newT);
document.body.appendChild(newA);

And then your bookmarklet link can be like this:

javascript:var jsCode = document.createElement('script');jsCode.setAttribute('src', 'http://localhost/bookmarklet.js?r='+Math.random()*99999999);document.body.appendChild(jsCode);

Alternatively, you need to include the popup in the actual bookmarklet link. Which in turn will mean that the only way to make any changes is for the user to re-install the bookmarklet.

EDIT: In addition to the above method, I later found that there's even another way to work around this by using easyXDM. It can help you work around the Same Origin Policy http://easyxdm.net/wp/

Using this, you can use an iframe for your bookmarklet and you can even have a "close" link inside your iframe that will be able to remove the iframe from the parent page.

这篇关于小书签如何避免弹出式窗口拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆