chrome extensions 开发

准备

先找资料 chrome extensions 有官方的.但是我的english么,先不折磨自己了.

猎豹对官方的doc做了翻译:Chrome 扩展技术手册 ,一看这独特的行文,怎么这么熟.找找作者,果然是大妈Zoom.Quiet的大作.

Zoom.Quiet不是金山的么,看了下猎豹,才知道这是金山搞的的东东.

大汗的,看来长期用linux,对windows下的软件很容易孤陋寡闻.

哎,跑题了,不管怎么说,资料找到了.

明天再接着学.

Hello, World!

搞个json到目录里,就可以装个插件了说

{
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
    "http://api.flickr.com/"
  ]
}

来点功能

{
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "http://api.flickr.com/"
  ]
}

记得要到插件管理界面,点reload

例子的图片失效了.来这里下最新的.

bigmarks

用上面的例子慢慢改 这里有现成的例子下载

api

https://developer.chrome.com/extensions/api_index

调试

右健点图标,选inspect popup,就可以看到插件的js里打旳log了.

和网页的调试没什么区别.

取到bookmarks

要声明bookmarks的权限 { “name”: “bigmarks”, “version”: “1.0”, “manifest_version”: 2, “description”: “用来和挖站同步收藏的书签”, “browser_action”: { “default_icon”: “icon.png”, “default_popup”: “popup.html” }, “permissions”: [ “bookmarks” ] } js如下

    chrome.bookmarks.getTree(
        function(bookmarkTreeNodes) {
            console.log(bookmarkTreeNodes);
        });

这样就取到了,很简单.

事件触发

改变bookmark的时候,触发同步

function getBookMarks(){
    chrome.bookmarks.getTree(
        function(bookmarkTreeNodes) {
            console.log(bookmarkTreeNodes);
        });
}
chrome.bookmarks.onMoved.addListener(getBookMarks);
chrome.bookmarks.onRemoved.addListener(getBookMarks);
chrome.bookmarks.onCreated.addListener(getBookMarks);
//只有title和url改变时会触发
chrome.bookmarks.onChanged.addListener(getBookMarks);

对应的id和bookmarks都能得到

function show(id, bookmarks){
    console.log(id);
    console.log(bookmarks);
}
chrome.bookmarks.onMoved.addListener(show);
chrome.bookmarks.onRemoved.addListener(show);
chrome.bookmarks.onCreated.addListener(show);
//只有title和url改变时会触发
chrome.bookmarks.onChanged.addListener(show);