From dfd8a1221c83f364a880cd3e7dd2f3d0ab4d91ac Mon Sep 17 00:00:00 2001 From: harveyshao Date: Wed, 14 Dec 2022 13:41:55 +0800 Subject: [PATCH] opt chrome --- frame.js | 1 + manifest.json | 2 +- page/index.css | 1 + proto.js | 4 +- publish/chrome/chrome.js | 64 -------------------- publish/chrome/contexts.js | 67 ++++++++++----------- publish/chrome/{chrome.html => daemon.html} | 3 +- publish/chrome/daemon.js | 57 ++++++++++++++++++ publish/chrome/popup.html | 2 +- 9 files changed, 95 insertions(+), 106 deletions(-) delete mode 100644 publish/chrome/chrome.js rename publish/chrome/{chrome.html => daemon.html} (70%) create mode 100644 publish/chrome/daemon.js diff --git a/frame.js b/frame.js index e82bab28..7e533840 100644 --- a/frame.js +++ b/frame.js @@ -141,6 +141,7 @@ Volcanos(chat.ONAPPEND, {_init: function(can, meta, list, cb, target, field) { can.core.Item(meta.feature, function(key, cb) { cb.help && sub.user.trans(sub, kit.Dict(key, cb.help)) }) meta.inputs && sub.onappend._option(sub, meta, sub._option, meta.msg) if (meta.msg) { var msg = sub.request(); msg.Copy(can.base.Obj(meta.msg)), sub.onappend._output(sub, msg, meta.display||msg.Option(ice.MSG_DISPLAY)||meta.feature.display) } + if (can._root && can._root.name == "popup") { can.onmotion.hidden(can, sub._action) } can.core.Value(sub._legend, chat.ONMOUSEENTER, function(event) { can.user.carte(event, sub, sub.onaction, sub.onaction.list.concat([[ctx.ACTION].concat(can.core.Item(meta.feature._trans))]), function(event, button, meta) { diff --git a/manifest.json b/manifest.json index d308448f..086140f4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "volcanos", "version": "0.0.1", - "background": {"page": "/publish/chrome/chrome.html"}, + "background": {"page": "/publish/chrome/daemon.html"}, "browser_action": {"default_popup": "/publish/chrome/popup.html"}, "content_scripts": [{"matches": [""], "permissions": [ "tabs", "history", "cookies", "bookmarks", "contextMenus", "notifications", "http://localhost:9020/*" diff --git a/page/index.css b/page/index.css index 3ad12c17..b34969ef 100644 --- a/page/index.css +++ b/page/index.css @@ -188,6 +188,7 @@ body.white fieldset.plugin { background-color:#ffffffa1; } body.white fieldset.float { background-color:#0e3369; color:white; } body.white fieldset.full { background-color:#0e3369; color:white; } body.white div.output div.project div.zone>div.list>div.zone>div.name { background-color:#09466f66; color:black; } +body.white fieldset.full div.output div.project div.zone>div.list>div.zone>div.name { background-color:#09466f66; color:white; } /* white hover */ body.white legend:hover { background-color:skyblue; } diff --git a/proto.js b/proto.js index 637e2b1a..0d691767 100644 --- a/proto.js +++ b/proto.js @@ -256,11 +256,11 @@ function shy(help, meta, list, cb) { var args = arguments, i = 0; function next( var Volcanos = shy({iceberg: "/chat/", volcano: "/frame.js", cache: {}, pack: {}}, function(name, can, libs, cb) { var meta = arguments.callee.meta, list = arguments.callee.list; if (typeof name == lang.OBJECT) { if (name.length > 0) { return Volcanos({panels: [{name: chat.HEADER, pos: html.HIDE, state: [aaa.USERNICK]}, {name: chat.ACTION, pos: html.MAIN, tool: name}]}) } - var Config = name; name = Config.name||ice.CAN, kit.proto(meta, Config), _can_name = "", _can_path = "" + var Config = name; name = Config.name||ice.CAN, _can_name = "", _can_path = "" meta.iceberg = Config.iceberg||meta.iceberg, meta.libs = Config.libs||chat.libs, panels = Config.panels||chat.panel_list libs = [], panels.forEach(function(p) { p && (libs = libs.concat(p.list = p.list||["/panel/"+p.name+nfs._JS, "/panel/"+p.name+nfs._CSS])) }), libs = libs.concat(Config.plugin||chat.plugin_list) cb = can||function(can) { can.onengine._init(can, can.Conf(Config), panels, Config._init||meta._init, can._target) } - can = {_follow: name, _target: Config.target||meta.target, _height: Config.height||meta._height, _width: Config.width||meta._width} + can = Config, can._follow = name, can._target = Config.target||meta.target, can._height = Config.height||meta._height, can._width = Config.width||meta._width } can = kit.proto(can||{}, kit.proto({_path: _can_path, _name: name, _load: function(name, cbs) { var cache = meta.cache[name]||[] for (list.reverse(); list.length > 0; list) { var sub = list.pop(); sub != can && cache.push(sub), sub._path = name } meta.cache[name] = cache diff --git a/publish/chrome/chrome.js b/publish/chrome/chrome.js deleted file mode 100644 index 400265a2..00000000 --- a/publish/chrome/chrome.js +++ /dev/null @@ -1,64 +0,0 @@ -Volcanos({ - chrome: function(can, msg, arg, cb) { - if (arg.length == 0 || arg[0] == "") { // 窗口列表 - chrome.windows.getAll(function(wins) { - can.core.List(wins, function(win) { win.wid = win.id - msg.Push(win, ["wid", "state", html.LEFT, html.TOP, html.WIDTH, html.HEIGHT]) - }), can.base.isFunc(cb) && cb(msg) - }) - } else if (arg.length == 1 || arg[1] == "") { // 标签列表 - chrome.tabs.getAllInWindow(parseInt(arg[0]), function(tabs) { - can.core.List(tabs, function(tab) { tab.tid = tab.id - msg.Push(tab, ["tid", "active", html.WIDTH, html.HEIGHT, "index", "title", "url"]) - }), can.base.isFunc(cb) && cb(msg) - }) - } else if (arg[1] == "current") { // 当前标签 - chrome.tabs.query({currentWindow: true, active: true}, function(tabs) { arg[1] = tabs[0].id - chrome.tabs.sendMessage(parseInt(arg[1]), msg, function(res) { - can.base.isFunc(cb) && cb(msg.Copy(res)) - }) - }) - } else { // 下发命令 - chrome.tabs.sendMessage(parseInt(arg[1]), msg, function(res) { - can.base.isFunc(cb) && cb(msg.Copy(res)) - }) - } - }, - _daemon: function(can) { - can.misc.WSS(can, {type: html.CHROME, name: html.CHROME}, function(event, msg, cmd, arg) { - if (msg.Option(ice.MSG_TARGET)) { msg.detail = ["", "", ""].concat(msg.detail) - chrome.tabs.sendMessage(parseInt(msg.Option(ice.MSG_TARGET)), msg, function(res) { - msg.Copy(res), msg.Reply() - }) - return - } - can.core.CallFunc([can, cmd], {can: can, msg: msg, arg: arg, cb: function() { msg.Reply() }}) - }) - chrome.runtime.onMessage.addListener(function(req, sender, cb) { - var msg = can.request({}, {tid: sender.tab.id, url: sender.url}) - can.core.List(req.option, function(key) { msg.Option(key, req[key][0]) }) - msg.__daemon = can.core.Keys(html.CHROME, sender.tab.id) - can.run(msg, req.detail||[], cb) - return true - }) - chrome.history.onVisited.addListener(function(item) { - can.run({}, ["sync", mdb.TYPE, "link", mdb.NAME, item.title, mdb.LINK, item.url, "tid", item.id]) - }) - }, - _motion: function(can) { - can.user.toast = function(can, message, title) { chrome.notifications.create(null, { - message: message, title: title||can._name, iconUrl: "/favicon.ico", type: "basic", - })}, - chrome.contextMenus.create({title: "volcanos", onclick: function(event) { - chrome.tabs.query({currentWindow: true, active: true}, function(tabs) { - var msg = can.request(event); msg.detail = [html.CHROME, "", "", "order"] - chrome.tabs.sendMessage(tabs[0].id, msg) - }) - }}) - }, -}, function(can) { - can.run = function(event, cmds, cb) { var msg = can.request(event) - can.misc.Run(event, can, {names: "http://localhost:9020/code/chrome/"+cmds[0]}, cmds.slice(1), cb) - }, can._daemon(can), can._motion(can) -}) - diff --git a/publish/chrome/contexts.js b/publish/chrome/contexts.js index 1c580d9c..9d2d3ae1 100644 --- a/publish/chrome/contexts.js +++ b/publish/chrome/contexts.js @@ -1,42 +1,36 @@ -setTimeout(function() { Volcanos({Option: function() { return [] }, - spide: function(can, msg, _target) { - if (!_target) { +setTimeout(function() { Volcanos({ + Option: function() { return [] }, + spide: function(can, msg, target) { + if (!target) { msg.Push(mdb.TYPE, mdb.LINK) msg.Push(mdb.NAME, document.title) msg.Push(mdb.LINK, location.href) } - - var has = {}; _target = _target||document.body - can.page.Select(can, _target, html.IFRAME, function(item) { - if (!item.src || has[item.src]) { return } has[item.src] = true - + var has = {}; target = target||document.body + can.page.Select(can, target, html.IFRAME, function(target) { + if (!target.src || has[target.src]) { return } has[target.src] = true msg.Push(mdb.TYPE, html.IFRAME) msg.Push(mdb.NAME, "") - msg.Push(mdb.LINK, item.src) - - can.spide(can, msg, item.contentWindow.document.body) + msg.Push(mdb.LINK, target.src) + can.spide(can, msg, target.contentWindow.document.body) }) - can.page.Select(can, _target, html.VIDEO, function(item) { - if (!item.src || has[item.src]) { return } has[item.src] = true - var name = item.src.split("?")[0].split(ice.PT).pop() - var p = can.page.Select(can, _target, "p.title")[0] - + can.page.Select(can, target, html.VIDEO, function(target) { + if (!target.src || has[target.src]) { return } has[target.src] = true + var name = target.src.split("?")[0].split(ice.PT).pop() msg.Push(mdb.TYPE, html.VIDEO) - msg.Push(mdb.NAME, (p && p.innerText || html.VIDEO)+ice.PT+name) - msg.Push(mdb.LINK, item.src) + msg.Push(mdb.NAME, html.VIDEO+ice.PT+name) + msg.Push(mdb.LINK, target.src) }) - - can.page.Select(can, _target, html.IMG, function(item) { - if (!item.src || has[item.src]) { return } has[item.src] = true - var name = item.src.split("?")[0].split(ice.PS).pop() - + can.page.Select(can, target, html.IMG, function(target) { + if (!target.src || has[target.src]) { return } has[target.src] = true + var name = target.src.split("?")[0].split(ice.PS).pop() msg.Push(mdb.TYPE, html.IMG) - if (item.src.indexOf("data:image") == 0) { - msg.Push(mdb.NAME, item.src.slice(item.src.length-20)) + if (target.src.indexOf("data:image") == 0) { + msg.Push(mdb.NAME, target.src.slice(target.src.length-20)) } else { msg.Push(mdb.NAME, name||"image.jpg") } - msg.Push(mdb.LINK, item.src) + msg.Push(mdb.LINK, target.src) }) }, change: function(can, msg, arg) { @@ -97,23 +91,24 @@ setTimeout(function() { Volcanos({Option: function() { return [] }, }) }, + info: function(can, msg, arg) { + msg.Push("title", document.title) + msg.Push("url", location.href) + }, _daemon: function(can) { chrome.extension.onMessage.addListener(function(req, sender, cb) { var msg = can.request(); msg.Copy(req); can.misc.Log(req.detail, msg) - can.core.CallFunc([can, req.detail[3]||"spide"], {can: can, msg: msg, cmds: req.detail.slice(4), arg: req.detail.slice(4), cb: function() { + can.core.CallFunc([can, req.detail[0]||"spide"], {can: can, msg: msg, cmds: req.detail.slice(1), arg: req.detail.slice(1), cb: function() { delete(msg._event), delete(msg._can), cb(msg) }}) }) }, - _motion: function(can) { can.onmotion.float.auto(can, document.body) - document.body.ondblclick = function(event) { can.onengine.signal(can, "onselection") } - - can.runAction({}, ctx.COMMAND, [], function(msg) { - msg.result && msg.result[0] && can.field(can, msg, msg.result) - }) - }, + _motion: function(can) { can.onmotion.story.auto(can, document.body) + document.body.ondblclick = function(event) { can.onengine.signal(can, "onselection") } + can.runAction({}, ctx.COMMAND, [], function(msg) { msg.result && msg.result[0] && can.field(can, msg, msg.result) }) + }, }, function(can) { can.run = function(event, cmds, cb) { if (cmds[0] == "_search") { return } var msg = can.request(event, {host: location.host}); msg.detail = can.misc.concat(can, ["page"], cmds) chrome.runtime.sendMessage(msg, function(res) { can.base.isFunc(cb) && cb(msg.Copy(res)) }) - }, can._daemon(can), can._motion(can) -}) }, 1) + }, can._motion(can), can._daemon(can) +}) }, 100) diff --git a/publish/chrome/chrome.html b/publish/chrome/daemon.html similarity index 70% rename from publish/chrome/chrome.html rename to publish/chrome/daemon.html index 6650eb04..b8e19152 100644 --- a/publish/chrome/chrome.html +++ b/publish/chrome/daemon.html @@ -3,7 +3,6 @@ - + - diff --git a/publish/chrome/daemon.js b/publish/chrome/daemon.js new file mode 100644 index 00000000..ab892a0e --- /dev/null +++ b/publish/chrome/daemon.js @@ -0,0 +1,57 @@ +Volcanos({ + chrome: function(can, msg, arg, cb) { msg.detail = msg.detail.slice(3) + if (arg.length == 0 || arg[0] == "") { + chrome.windows.getAll(function(wins) { + can.core.List(wins, function(win) { win.wid = win.id + msg.Push(win, ["wid", "type", "state", "focused", html.LEFT||"0", html.TOP||"0", html.WIDTH, html.HEIGHT]) + }), can.base.isFunc(cb) && cb(msg) + }) + } else if (arg.length == 1 || arg[1] == "") { + chrome.tabs.getAllInWindow(parseInt(arg[0]), function(tabs) { + can.core.Next(tabs, function(tab, next) { var _msg = can.request(); _msg.detail = ["info"] + can._tabsend(can, _msg, function(res) { tab.tid = tab.id + msg.Push(tab, ["tid", "active", html.WIDTH, html.HEIGHT, "index"]) + msg.Push("title", res && res["title"][0] || "") + msg.Push("url", res && res["url"][0] || "") + next() + }, tab.id) + }, function() { can.base.isFunc(cb) && cb(msg) }) + }) + } else if (arg[1] == "current") { + chrome.tabs.query({currentWindow: true, active: true}, function(tabs) { arg[1] = tabs[0].id + can._tabsend(can, msg, null, arg[1]) + }) + } else { + can._tabsend(can, msg, null, arg[1]) + } + }, + _tabsend: function(can, msg, cb, target) { + chrome.tabs.sendMessage(parseInt(target||msg.Option(ice.MSG_TARGET)), msg, cb||function(res) { msg.Copy(res), msg.Reply() }) + }, + _daemon: function(can) { + can.misc.WSS(can, {type: html.CHROME, name: html.CHROME}, function(event, msg, cmd, arg, cb) { + msg.Option(ice.MSG_TARGET)? can._tabsend(can, msg): can.core.CallFunc([can, cmd], {can: can, msg: msg, arg: arg, cb: cb}) + }) + chrome.runtime.onMessage.addListener(function(req, sender, cb) { + var msg = can.request({}, {tid: sender.tab.id, url: sender.url}); can.core.List(req.option, function(key) { msg.Option(key, req[key][0]) }) + return msg._source = [sender.tab.id], can.run(msg, req.detail||[], cb), true + }) + chrome.history && chrome.history.onVisited.addListener(function(item) { + can.run({}, ["sync", mdb.TYPE, mdb.LINK, mdb.NAME, item.title, mdb.LINK, item.url, "tid", item.id]) + }) + }, + _motion: function(can) { + can.user.toast = function(can, message, title) { chrome.notifications.create(null, { + message: message, title: title||can._name, iconUrl: "/favicon.ico", type: "basic", + })} + chrome.contextMenus && chrome.contextMenus.create({title: "volcanos", onclick: function(event) { + chrome.tabs.query({currentWindow: true, active: true}, function(tabs) { + var msg = can.request(event); msg.detail = ["order"], can._tabsend(can, msg, function() {}) + }) + }}) + }, +}, function(can) { + can.run = function(event, cmds, cb) { can.misc.Run(event, can, {names: "http://localhost:9020/code/chrome/"+cmds[0]}, cmds.slice(1), cb) } + can._motion(can), can._daemon(can) +}) + diff --git a/publish/chrome/popup.html b/publish/chrome/popup.html index 8fa22600..3320a612 100644 --- a/publish/chrome/popup.html +++ b/publish/chrome/popup.html @@ -3,7 +3,7 @@ - +