UNPKG

fastchar-dom-plugin

Version:

Chrome插件,用于操作HTML的DOM

140 lines (139 loc) 5.24 kB
define(["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FastPopup = void 0; /** * 插件popup页面操作工具类,在插件的popup页面中引用本工具类,可直接操作内容页面的DOM结构! */ var FastPopup; (function (FastPopup) { /** * 内容页面核心操作 */ var Core = /** @class */ (function () { function Core() { Core.popupId = Core.buildUDID(); } /** * 构建唯一标识符 * @private */ Core.buildUDID = function () { var d = new Date().getTime(); return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); }; /** * 开始发起连接 */ Core.startConnect = function () { this.started = true; chrome.tabs.query({ active: true }, function (tab) { // @ts-ignore FastPopup.Core.connectPort = chrome.tabs.connect(tab[0].id, { name: "FastCharPopup@" }); FastPopup.Core.connectPort.onMessage.addListener(function (msg) { if (msg.event && msg.event === "OnReturnMethod") { FastPopup.Core.returnMethod(msg); } }); }); }; /** * 发送消息 * @param data * @private */ Core.postMessage = function (data) { if (!this.started) { Core.startConnect(); } if (this.connectPort) { this.connectPort.postMessage(data); } }; /** * 执行方法 * @param data * @param callback */ Core.invokeMethod = function (data, callback) { data["event"] = "OnInvokeMethod"; data["popupId"] = Core.popupId; data["callbackId"] = this.buildUDID(); this.callbackMap[data.callbackId] = callback; this.postMessage(data); }; /** * 响应方法 * @param msg */ Core.returnMethod = function (msg) { if (msg.callbackId && this.callbackMap[msg.callbackId]) { this.callbackMap[msg.callbackId](msg.data); } }; Core.callbackMap = {}; Core.started = false; return Core; }()); FastPopup.Core = Core; /** * 内容页面Element操作 */ var Element = /** @class */ (function () { function Element() { } /** * 获取节点的网页内容 * @param selector jquery选择器 * @param callback 回调函数 */ Element.getHtml = function (selector, callback) { Core.invokeMethod({ method: "html", selectors: [selector], params: [] }, callback); }; /** * 获取节点的文本内容 * @param selector jquery选择器 * @param callback 回调函数 */ Element.getText = function (selector, callback) { Core.invokeMethod({ method: "text", selectors: [selector], params: [] }, callback); }; /** * 设置目标节点value值 * @param selector jquery选择器 * @param value 设置的值 * @param callback 回调函数 */ Element.setValue = function (selector, value, callback) { Core.invokeMethod({ method: "val", selectors: [selector], params: [value] }, callback); }; /** * 获取目标节点的value值 * @param selector jquery选择器 * @param callback 回调函数 */ Element.getValue = function (selector, callback) { Core.invokeMethod({ method: "val", selectors: [selector], params: [] }, callback); }; /** * 点击目标节点 * @param selector jquery选择器 * @param callback 回调函数 */ Element.click = function (selector, callback) { Core.invokeMethod({ method: "click", selectors: [selector], params: [] }, callback); }; return Element; }()); FastPopup.Element = Element; for (var subClass in FastPopup) { // @ts-ignore FastPopup[subClass](); } window["FastPopup"] = FastPopup; })(FastPopup = exports.FastPopup || (exports.FastPopup = {})); });