在网页中作为Google Chrome扩展程序自动加载书签 [英] Auto-load bookmarklet when in webpage as a Google Chrome extension

查看:252
本文介绍了在网页中作为Google Chrome扩展程序自动加载书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我做了一个书签,并且当打开某个通配符的URL时,我希望它运行。由于某些原因,它不会简单地在Chrome扩展中作为JavaScript运行,并且我厌倦了尝试。

Basically I made a bookmarklet, and I'd like it to run when a certain wildcarded URL is opened. For some reasons it won't simply run as javascript in a chrome extension, and I'm tired of trying.

我认为 可以工作就是为指定页面创建一个扩展,其中包含 content_script (它允许通过 match )和以某种方式使它做同样的事情,如果用户点击书签栏中的书签

What I think could work is to make an extension that has a content_script for the specified page(s) (which allows a wildcard via match), and somehow make it do the same thing that would be done if the user clicked the bookmarklet in the bookmarks bar.

然而,我不知道如何做到这一点。

However, I do not know how to make this.

需要注意的一点是我需要它访问页面的全局范围,也就是说,的扩展沙箱(这是可能的,并且已被确认可以通过Chromium bug追踪器中的设计来实现)。

One thing to note is that I need it to access the page's global scope, i.e., break out of the extension sandbox (which is possible, and has been confirmed to be possible by design in the Chromium bug tracker).

所以问题再次是:,从 content_script ,是否加载书签(换句话说,如何将书签转换为G oogle Chrome扩展程序)。我也使用普通的JavaScript,如果这可能是有用的。

So the question again is: how, from an content_script, do I "load the bookmarklet" (in other words, how to convert a bookmarklet to a Google Chrome extension). I have it in plain javascript too, if that could be of use.

这是书签,以防有人想用它来测试。它的意思是用于 my.deviantart.com/messages / * (但是您需要在收件箱中添加一个帐户和邮件,才能将效果悬停在指向偏差的链接上,并且会显示工具提示及其缩略图)。

This is the bookmarklet, in case somebody wants to test with it. It's meant to be used at my.deviantart.com/messages/* (but you need an account and messages in your inbox, to see the effect hover on top of a link to a "deviation", and it will show a tooltip with a thumbnail of it).

编辑:这是一个扩展尝试,发布在答案的评论中)

( Here's an extension attempt, posted in an answer's comments)

推荐答案

如果您将网址放入权限是您的清单中的一部分...

http:// code。 google.com/chrome/extensions/xhr.html

You can make cross domain calls from a content script if you put a url in the permissions part of your manifest...
http://code.google.com/chrome/extensions/xhr.html

它似乎令人窒息的是您放入请求网址的回调,需要我把它拿出来。

这是您的代码的工作版本....

清单

What it seemed to be choking on was the callback that you put in the request url and thats not needed so I took it out.
Here's a working version of your code....
Manifest

{
  "name": "dA Tooltip Thumbnail",
  "version": "1.0.0",
  "description": "What the name says.",
  "permissions": [
    "http://backend.deviantart.com/*"
  ],
  "icons": {
    "48" : "sample-48.png",
    "128" : "sample-128.png"
  },
  "content_scripts": [
    {
      "matches": ["http://my.deviantart.com/messages/*"],
      "js" : ["jquery-1.7.1.min.js","contentscript.js"]
    }
  ]
}

ContentScript

$(".mcb-title a:first-child").each(function() {
    var b=$(this).attr("href");
    null!=b.match(/https?:\/\/fav\.me\/.*|https?:\/\/.*\.deviantart\.com\/art.*/)&&"true"!=$(this).attr("da-message-preview-attached")&&$.getJSON("http://backend.deviantart.com/oembed?url="+encodeURIComponent(b),$.proxy(function(b) {
        $(this).addClass("da-message-preview").attr("rel",b.thumbnail_url).attr("da-message-preview-attached","true");
        $(this).hover(function(a) {
            window.daMessagePreviewTitle=this.title;
            this.title="";
            $("body").append('<p id="da-message-preview"><img src="'+this.rel+'"/></p>');
            $("#da-message-preview").css( {top:a.pageY-10+"px",left:a.pageX+30+"px",position:"absolute",border:"1px solid #666",background:"#EEE",padding:"5px",display:"none","-webkit-border-radius":"6px","-moz-border-radius":"6px","border-radius":"6px","-webkit-box-shadow":"0px 2px 8px #000","-moz-box-shadow":"0px 2px 8px #000","box-shadow":"0px 2px 8px #000","z-index":"123456"}).fadeIn("fast")
        },function() {
            $("#da-message-preview").remove()
        });
        $(this).mousemove(function(a) {
            $("#da-message-preview").css("top",a.pageY-10+"px").css("left",a.pageX+30+"px")
        })
    },this))

});  

我在修改之后发现的唯一错误是它试图获取一个获得404的url .. 。

http://backend.deviantart.com/oembed?url=http%3A%2F%2Fnews.deviantart.com%2Farticle%2F143885%2F

...小错误,Ill把它留给你去摆脱那个;)。
哦,我拿出定时器的东西,是真的需要吗?当你点击一个画廊时,你不会去一个不同的URL?......因为如果你这样做,那么内容脚本将被重新注入(你可能需要添加更多的匹配,没有看到)。

The only error I noticed after the changes was it tries to get a url that gets a 404...
http://backend.deviantart.com/oembed?url=http%3A%2F%2Fnews.deviantart.com%2Farticle%2F143885%2F
...small error, Ill leave it up to you to get rid of that one ;).
OH, and I took out the timer stuff, is that really needed? Wont you be going to a different url when you click on a gallery?...because if you do then the content script will get reinjected (you may need to add more matches for that tho, didnt really look).

这篇关于在网页中作为Google Chrome扩展程序自动加载书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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