简体   繁体   English

将网站图标添加到 javascript Bookmarklet(使用 window.open)

[英]Adding favicon to javascript Bookmarklet (uses window.open)

I have a bookmarklet that launches a window.open javascript function to open a small window with my bookmarklet -- an external feature used to communicate between any visted site and my server.我有一个书签,它启动一个 window.open javascript 函数,用我的书签打开一个小窗口——一个用于在任何访问过的站点和我的服务器之间进行通信的外部功能。 I'd like for a favicon to show up when the bookmarklet is added to the bookmark toolbar.我希望在将书签添加到书签工具栏时显示收藏夹图标 I realize that the bookmarklet is javascript, there is no domain tied to it so it's going to be either difficult or impossible to achieve this goal.我意识到书签是 javascript,没有与它相关的域,所以要实现这个目标要么很困难,要么不可能。

My understanding of the problem:我对问题的理解:

Favicons are easy to understand, a link within the head of an HTML doc.图标很容易理解,是 HTML 文档头部的一个链接。 The browser can pull this when bookmarking an actual site by reference.当通过引用为实际站点添加书签时,浏览器可以拉取它。 However, as you see my bookmarklet is ran off a javascript launch code where there exists no HTML, therefor no link to a favicon .但是,正如您所看到的,我的书签是从不存在 HTML 的 javascript 启动代码中运行的,因此没有指向favicon 的链接。 I'm not ready to give up yet though, I feel that there's some injection that can be made...不过我还没有准备好放弃,我觉得可以进行一些注射......

As of now, the bookmarklet launch code looks like this:截至目前,书签启动代码如下所示:

Current Script -- bookmarklet, no favicon (note all code is formated with line breaks -- won't work in all browsers, normally its one line)当前脚本——书签,没有图标(注意所有代码都用换行符格式化——不适用于所有浏览器,通常只有一行)

javascript:void(window.open(
        'http://mydomain.com/bookmarklet/form?u='
        +encodeURIComponent(location.href)+
        't='+encodeURIComponent(document.title),
        'test','status=0,toolbar=0,location=0,menubar=0,
         resizable=false,scrollbars=false,height=379,width=379'
        ));

The closest thing I've found to a solution is as follows, but it doesn't open a new window -- just creates a new tab with the html as the page:我发现的最接近解决方案的事情如下,但它不会打开一个新窗口 - 只是创建一个带有 html 作为页面的新选项卡:

Working favicon, no bookmarklet window工作图标,没有书签窗口

javascript:'<!DOCTYPE html>
    <html><head>
    <title>Hello World</title>
    <link rel="icon" type="image/png" href="http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png" />
    </head>
    <body>Hello World</body>
    </html>';

I have tried a combination of the two but it didn't seem to use the icon.我尝试了两者的组合,但它似乎没有使用该图标。 I'd be curious to know if anyone can see a type of workaround.. I think it could be possible, I just don't think it's set up correctly as I've been trying.我很想知道是否有人可以看到一种解决方法..我认为这是可能的,我只是认为它的设置不正确,因为我一直在尝试。

My hybrid of the two -- bookmarklet but no favicon我的两者的混合——书签但没有网站图标

javascript:'<!DOCTYPE html>
    <html><head>
    <title>Hello World</title>
    <link rel="icon" type="image/png" href="http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png" />
    </head><body>Hello World</body></html>';
    window.open('http://mydomain.com/bookmarklet/form?u='
    +encodeURIComponent(location.href)+
    '&t='+encodeURIComponent(document.title),
    'test',
    'status=0,toolbar=0,location=0,menubar=0,resizable=false,
     scrollbars=false,height=379,width=379').void(0);

What I did was use the html structure before firing window.open(), this successfully opened my bookmarklet in a new window, but no favicon showed up for the bookmark icon.我所做的是在触发 window.open() 之前使用 html 结构,这成功地在新窗口中打开了我的书签,但书签图标没有显示图标。


Logical Solution:逻辑解决方案:

My thoughts on this would be to have the bookmarklet point to a page that is simply an HTML file with a favicon link and the launch script in the <head> .我对此的想法是让书签指向一个页面,该页面只是一个带有网站图标链接和<head>的启动脚本的 HTML 文件。 However, I don't want this opening in a new tab with a blank HTML file that then launches a popup.. Workaround..?但是,我不希望此操作在带有空白 HTML 文件的新选项卡中打开,然后启动弹出窗口.. 解决方法..?


There exists a similar question but I did not seem to find the answer I'm looking for:存在一个类似的问题,但我似乎没有找到我正在寻找的答案:

How to have favicon / icon set when bookmarklet dragged to toolbar? 将书签拖到工具栏时如何设置收藏夹图标/图标?

Source for the working javascript favicon (no bookmarklet however):工作 javascript favicon 的来源(但是没有书签):

http://www.tapper-ware.net/blog/?p=97 http://www.tapper-ware.net/blog/?p=97

I'd be interested in what your current knowledge/thoughts on this would be我会对你目前对此的知识/想法感兴趣

Some of the things that I've tried that might possibly get you going a bit more:我尝试过的一些事情可能会让你走得更远:

Append a new link element to the current document:向当前文档追加一个新的链接元素:

javascript: var newLink = document.createElement('link');
newLink.setAttribute('rel','icon');
newLink.setAttribute('type','image/png');
newLink.setAttribute('href','http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png');
document.querySelector('head').appendChild(newLink);
void(0);

Note that I was using the querySelector due to IE testing (though works in modern browsers as well).请注意,由于 IE 测试,我使用了 querySelector(尽管也适用于现代浏览器)。 With Chrome and FF, I kept getting invalid character when trying to create the element, so I had to do piecewise attribute setting.使用 Chrome 和 FF 时,我在尝试创建元素时不断收到无效字符,因此我不得不进行分段属性设置。

Tried using base64 encoded image string using the "data:image/png;base64,iVBORw0KGgoAAAA..." URI schema, but that didn't help anything due to the fact that I still had to set it to the current HTML text (which I could do, but ran into the same problem as you above of no bookmarklet).尝试使用 "data:image/png;base64,iVBORw0KGgoAAAA..." URI 模式使用 base64 编码的图像字符串,但这没有任何帮助,因为我仍然必须将其设置为当前的 HTML 文本(其中我可以做,但是遇到了与您上面没有书签的问题相同的问题)。

Maybe this can't be done due to cross site scripting concerns?也许由于跨站点脚本问题而无法做到这一点? Not sure... Either way, really curious to see what you come up with (if you manage to come up with anything).不确定......不管怎样,真的很想知道你想出了什么(如果你能想出什么的话)。

I tried and retried, and my first conclusion was: "It can't be done (at least not in FF4 on Ubuntu 11.04)".我试了又试,我的第一个结论是:“它不能完成(至少在 Ubuntu 11.04 的 FF4 中不能)”。 You need (I guess) a simple solution for your site visitors (drag&drop, add bookmark with 1 click ...).您需要(我猜)为您的网站访问者提供一个简单的解决方案(拖放、一键添加书签......)。

I have found a workaround, it does it's job, but it is a little buggy (maybe someone can help fix it).我找到了一个解决方法,它可以完成它的工作,但它有点问题(也许有人可以帮助修复它)。
PROS:优点:

  • add a icon to the bookmarklet向书签添加图标
  • it uses windows.open它使用 windows.open
  • doesn't leave empty pages behind不会留下空页

CONS:缺点:

  • it reloads the current page (instead of leave a page behind)它重新加载当前页面(而不是留下一个页面)
  • Can't make Firefox POP-ul blocker allow "javascript:" generated HTML page to load POP-ups, so you need to hit allow every time无法让 Firefox POP-ul 拦截器允许“javascript:”生成的 HTML 页面加载 POP-ups,因此您每次都需要点击允许

This is the code:这是代码:

<a href="javascript:'&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;link rel=&quot;icon&quot; type=&quot;image/png&quot; href=&quot;http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png&quot; /&gt;&lt;/head&gt;&lt;body onLoad=&quot;window.open(\\'http://example.com',\\'test\\',\\'status=0,toolbar=0,location=0,menubar=0,resizable=false,scrollbars=false,height=379,width=379\\');setTimeout(\\'history.back(-1);\\',100);&quot;&gt;&lt;/body&gt;&lt;/html&gt;';">Bookmarklet</a>

This is a link that you put on your page, the user needs to drag&drop this link to the bookmark bar (you can use something like Add Bookmark Script for adding it as a bookmark with 1 click), The bookmark has no icon until the user click's it at least once.这是您放在页面上的链接,用户需要将此链接拖放到书签栏(您可以使用诸如添加书签脚本之类的东西,只需单击一下即可将其添加为书签),书签在用户之前没有图标单击它至少一次。

So how it supose to work:那么它是如何工作的:
1. redirect the user to the generated HTML page from the bookmarklet (that makes the ICON posible) 1. 将用户从书签重定向到生成的 HTML 页面(这使得ICON 成为可能)
2. onLoad open the window you need using " windows.open " 2. onLoad 使用“ windows.open ”打开你需要的窗口
3. redirect the page back using " history.back(-1) " 3. 使用“ history.back(-1) ”重定向页面

In theory everithing happens so fast, that the user does't see the new page, just that the current page is reloading, and a new windows appear.从理论上讲,一切都发生得如此之快,以至于用户看不到新页面,只是当前页面正在重新加载,并出现一个新窗口。

The problem:问题:
1. I use setTimeout for history.back beacause window.open is blocked by Firefox, so I need to click allow every single time (if somebody can fix this ... we have a chance of using this, develop it further :) ) 1. 我使用 setTimeout for history.back 因为 window.open 被 Firefox 阻止,所以我每次都需要点击允许(如果有人可以解决这个问题......我们有机会使用它,进一步开发它:))

I know THIS is not a reliable solution, but this is the only solution I've got so far.我知道这不是一个可靠的解决方案,但这是我迄今为止唯一的解决方案。

Hope this helps a little.希望这有所帮助。 :) :)

"I don't want this opening in a new tab with a blank HTML file that then launches a popup.. Workaround..?" “我不希望在带有空白 HTML 文件的新选项卡中打开此文件,然后启动弹出窗口.. 解决方法..?”

If what you after really is the visual effect, you can try launch the blank HTML in hidden iframe, then launch the javascript.如果您真正追求的是视觉效果,您可以尝试在隐藏的 iframe 中启动空白 HTML,然后启动 javascript。

Hope that helps希望有帮助

这可能不是你想要的解决方案,特别是如果你不使用Firefox,但是我使用一个名为Bookmark Favicon Changer的附加组件来设置我的bookmarklet上的图标并且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM