粉丝: 2
关注: 0
积分: 52
精华: 0
金币: 36
经验: 51
最后登录 2021-2-11
|
1.用谷歌浏览器调用相关API是正常的。
最小化复现步骤:
1.创建一个扩展,在manifest文件中生声明bookmarks权限.
2.添加几个书签(保证下面调用的书签id存在)。
3.在扩展的background.js文件中运行以下代码
- chrome.bookmarks.move("5", {
- parentId: '2',
- index: 0
- }, function() {});
复制代码
另外可以尝试安装Recursive Bookmark Sorter,审查扩展页后执行排序操作,会没有效果并且报错。
完整代码如下
background.js
- // Copyright (c) 2010 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- // A generic onclick callback function.
- function genericOnClick(info, tab) {
- console.log("item " + info.menuItemId + " was clicked");
- console.log("info: " + JSON.stringify(info));
- console.log("tab: " + JSON.stringify(tab));
- }
- const handleClick = (info, tab) => {
- console.log("tab: " + JSON.stringify(tab));
- chrome.bookmarks.getChildren('2', (data) => {
- const linkList = data.filter(item => !!item.url)
- const sortByHost = (a, b) => {
- try {
- const getHost = (url) => (new URL(url)).host
- const getDomain = (host) => host.split('.').reverse()[1]
- console.log(getDomain(getHost(a.url)))
- return getDomain(getHost(a.url)) > getDomain(getHost(b.url))
- } catch(e) {
- console.error(e)
- return false
- }
- }
- // chrome.bookmarks.move("2135", {index: 0, parentId: '1692'}, (data) => {
- // console.log(data)
- // })
- // linkList.sort(sortByHost)
- // linkList.forEach(item => {
- // if (item.url) {
- // chrome.bookmarks.move(item.id, {
- // index: 0,
- // })
- // }
- // })
- console.log(linkList)
- })
- chrome.bookmarks.move("5", {
- parentId: '2',
- index: 0
- }, function() {});
- }
- chrome.contextMenus.create({
- title: '书签按域名排序',
- contexts: ['page'],
- onclick: handleClick
- })
复制代码
manifest.js
- {
- "name": "Bookmark Plus",
- "description": "Some ehancements of bookmark",
- "version": "0.1",
- "permissions": ["contextMenus", "bookmarks"],
- "background": {
- "scripts": ["background.js"]
- },
- "manifest_version": 2
- }
复制代码
|
|
评论
直达楼层