|
|
°²×¿H5ÈÕÖ¾´òÓ¡ÔËÐйý³ÌÀý×Ó
- // 🍎½»Á÷QQȺ£º711841924£¨Èº£©
- // 🍎½»Á÷QQȺ£º528816639 £¨°²×¿ÄÚ²âȺ£©
- // ³õʼ»¯Ò»¸öactivityÒ³Ãæ
- var ac = new activity();
- ac.loadXML(`
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
- <WebView
- android:id="@+id/web"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" />
- </LinearLayout>
- `);
- // ÑÓ³ÙÈ·±£²¼¾Ö¼ÓÔØÍê³É
- sleep.millisecond(400);
- // »ñÈ¡ WebView ×é¼þ
- var web1 = ac.findWebViewById('web');
- // ½«WebView¶ÔÏó±©Â¶ÎªÈ«¾Ö±äÁ¿£¬¹©Ö÷½Å±¾Ê¹ÓÃ
- global.web1 = web1;
- // ¼ÓÔØ±¾µØ HTML Îļþ£¨Ê¹Óþø¶Ô·¾¶£©
- web1.url('/´úÂë/h5.html');
- // ÈýÃëµ¹¼ÆÊ±
- printl("\n3 ÃëºóÆô¶¯...");
- for (var i = 3; i > 0; i--) {
- printl(i + "...");
- sleep.millisecond(1000);
- }
- printl("Æô¶¯Íê³É£¡\n");
- // µ¼ÈëÈÕ־ǎӯ÷
- Import("LogBridge.js");
- // ³õʼ»¯ÈÕ־ǎӣ¨»á×èÈûµÈ´ýWebView¼ÓÔØÍê³É£©
- LogManagerH5.init(web1, ac);
- // ¶¨Òå H5 Öе÷Óõĺ¯Êý
- function test(arg) {
- LogManagerH5.info("ÎÒ±» H5 µ÷ÓÃÁË£¬²ÎÊýÊÇ: " + arg);
- }
- // ¶¨Òå·µ»Ø×ÀÃæµÄº¯Êý£¬¹©H5Ò³Ãæµ÷ÓÃ
- function returnToHome() {
- LogManagerH5.info("Ö´Ðзµ»Ø×ÀÃæ²Ù×÷");
- // °²×¿»·¾³Ïµķµ»Ø×ÀÃæ²Ù×÷
- if (typeof device !== 'undefined' && device.home) {
- device.home();
- } else {
- LogManagerH5.warn("·µ»Ø×ÀÃæ¹¦ÄÜÔÚµ±Ç°»·¾³²»¿ÉÓÃ");
- }
- }
- // ·¢ËͲâÊÔÈÕÖ¾µ½H5½çÃæ
- LogManagerH5.success("ϵͳÆô¶¯³É¹¦");
- LogManagerH5.info("WebViewÒѾÍÐ÷");
- // ============================================
- // µ¼Èë²¢Ö´ÐÐ PrintLogSystem ÑÝʾ
- // ============================================
- // µ¼Èë²¢Ö´ÐÐ PrintLogSystem ÑÝʾ
- Import("PrintLogSystem.js");
- // ÑÓ³ÙÖ´ÐУ¬È·±£Îļþ¼ÓÔØÍê³É
- sleep.millisecond(100);
- // Ö´ÐÐÑÝʾ£¨È·±£ PrintLogSystem.js ÒѼÓÔØ£©
- if (typeof runPrintLogSystemDemo === 'function') {
- runPrintLogSystemDemo();
- } else {
- LogManagerH5.error("PrintLogSystem.js ¼ÓÔØÊ§°Ü");
- }
- /**
- * ============================================
- * 🍎 printÈÕ־ϵͳÍêÕûʾÀý
- * ¹¦ÄÜ: ÈÕÖ¾¼Ç¼¡¢µ÷ÊÔÊä³ö¡¢»Öл¹ÜÀí
- * ½»Á÷QQȺ: 711841924 (Ⱥһ) / 528816639 (Æ»¹ûÄÚ²âȺ)
- * ============================================
- */
¸´ÖÆ´úÂë
- /**
- * ============================================
- * ÈÕ־ǎӯ÷ - ½«printÈÕÖ¾Êä³öµ½H5½çÃæ
- * °æ±¾: 1.1.0
- * ƽ̨: Android (AIWROK)
- * ¹¦ÄÜ: ÇŽÓprintÈÕ־ϵͳµ½ÏÖÓÐH5½çÃæ
- * ½»Á÷QQȺ: 711841924 (Ⱥһ) / 528816639 (°²×¿ÄÚ²âȺ)
- * ============================================
- */
- // 🍎½»Á÷QQȺ£º711841924£¨Èº£©
- // 🍎½»Á÷QQȺ£º528816639 £¨°²×¿ÄÚ²âȺ£©
- /**
- * ÑÓ³Ùº¯Êý (°²×¿»·¾³¼æÈÝ)
- * @param {number} ms - ÑÓ³ÙºÁÃëÊý
- */
- function _sleep(ms) {
- sleep.millisecond(ms);
- }
- /**
- * LogBridge - ÈÕ־ǎӯ÷
- * ½«printÈÕ־ϵͳµÄÊä³öÇŽӵ½Ö¸¶¨µÄWebView
- */
- var LogBridge = {
- webView: null,
- activity: null,
- isReady: false,
- pendingLogs: [],
- maxPendingLogs: 100,
-
- /**
- * ³õʼ»¯ÇŽӯ÷
- * @param {WebView} webView - Ä¿±êWebViewʵÀý
- * @param {Activity} activity - »î¶¯ÊµÀý
- */
- init: function(webView, activity) {
- this.webView = webView;
- this.activity = activity;
- this.isReady = false;
- this.pendingLogs = [];
-
- // ×èÈûµÈ´ýWebView¼ÓÔØÍê³É
- _sleep(800);
-
- this.isReady = true;
- this._flushPendingLogs();
- printl('ÈÕ־ǎӯ÷ÒѾÍÐ÷');
-
- return this;
- },
-
- /**
- * ·¢ËÍÈÕÖ¾µ½H5
- * @param {string} message - ÈÕÖ¾ÏûÏ¢
- * @param {string} level - ÈÕÖ¾¼¶±ð (info/success/warn/error/gold/debug)
- */
- log: function(message, level) {
- level = level || 'info';
-
- // Èç¹û»¹Ã»×¼±¸ºÃ£¬»º´æÈÕÖ¾
- if (!this.isReady || !this.webView) {
- if (this.pendingLogs.length < this.maxPendingLogs) {
- this.pendingLogs.push({
- message: message,
- level: level,
- time: Date.now()
- });
- }
- return;
- }
-
- this._sendToWebView(message, level);
- },
-
- /**
- * ˢлº´æµÄÈÕÖ¾
- */
- _flushPendingLogs: function() {
- if (this.pendingLogs.length === 0) return;
-
- printl('ÕýÔÚˢР' + this.pendingLogs.length + ' Ìõ»º´æÈÕÖ¾...');
-
- // ÖðÌõ·¢ËÍ»º´æµÄÈÕÖ¾
- while (this.pendingLogs.length > 0) {
- var log = this.pendingLogs.shift();
- this._sendToWebView(log.message, log.level);
- // СÑÓ³Ù±ÜÃâ×èÈû
- _sleep(10);
- }
- },
-
- /**
- * ʵ¼Ê·¢ËÍÈÕÖ¾µ½WebView
- */
- _sendToWebView: function(message, level) {
- try {
- if (!this.webView) return;
-
- // תÒåÏûÏ¢ÖеÄÌØÊâ×Ö·û
- var escapedMessage = message
- .replace(/\\/g, '\\\\')
- .replace(/'/g, "\\'")
- .replace(/"/g, '\\"')
- .replace(/\n/g, '\\n')
- .replace(/\r/g, '\\r')
- .replace(/\t/g, '\\t');
-
- // ¹¹½¨JavaScript´úÂë
- var js = "if(typeof addLog === 'function') { addLog('" + escapedMessage + "', '" + level + "'); }";
-
- // ±£´æµ±Ç°¶ÔÏóµÄÒýÓÃ
- var self = this;
-
- // ÔÚÖ÷Ïß³ÌÖ´ÐÐWebView²Ù×÷
- if (this.activity && typeof this.activity.runOnUiThread === 'function') {
- this.activity.runOnUiThread(function() {
- try {
- // ³¢ÊÔ¶àÖÖ·½Ê½Ö´ÐÐJavaScript
- if (typeof self.webView.evaluateJavaScript === 'function') {
- self.webView.evaluateJavaScript(js);
- } else if (typeof self.webView.loadUrl === 'function') {
- var url = 'javascript:' + encodeURIComponent(js);
- self.webView.loadUrl(url);
- } else if (typeof self.webView.stringByEvaluatingJavaScriptFromString === 'function') {
- self.webView.stringByEvaluatingJavaScriptFromString(js);
- } else {
- // °²×¿»·¾³Ïµı¸Ó÷½·¨
- try {
- self.webView.injectScript(js);
- } catch (e) {
- printl('WebViewÖ´ÐÐJavaScriptʧ°Ü: ' + e.message);
- }
- }
- } catch (e) {
- printl('·¢ËÍÈÕ־ʧ°Ü: ' + e.message);
- }
- });
- } else {
- // Èç¹ûûÓÐactivity¶ÔÏó£¬Ö±½ÓÖ´ÐУ¨¿ÉÄÜ»áʧ°Ü£©
- try {
- if (typeof this.webView.evaluateJavaScript === 'function') {
- this.webView.evaluateJavaScript(js);
- } else if (typeof this.webView.loadUrl === 'function') {
- var url = 'javascript:' + encodeURIComponent(js);
- this.webView.loadUrl(url);
- } else if (typeof this.webView.stringByEvaluatingJavaScriptFromString === 'function') {
- this.webView.stringByEvaluatingJavaScriptFromString(js);
- } else {
- // °²×¿»·¾³Ïµı¸Ó÷½·¨
- try {
- this.webView.injectScript(js);
- } catch (e) {
- printl('WebViewÖ´ÐÐJavaScriptʧ°Ü: ' + e.message);
- }
- }
- } catch (e) {
- printl('·¢ËÍÈÕ־ʧ°Ü: ' + e.message);
- }
- }
- } catch (e) {
- printl('·¢ËÍÈÕ־ʧ°Ü: ' + e.message);
- }
- },
-
- // ¿ì½Ý·½·¨
- info: function(message) { this.log(message, 'info'); },
- success: function(message) { this.log(message, 'success'); },
- warn: function(message) { this.log(message, 'warn'); },
- error: function(message) { this.log(message, 'error'); },
- gold: function(message) { this.log(message, 'gold'); },
- debug: function(message) { this.log(message, 'debug'); }
- };
- /**
- * ÔöÇ¿°æLogManager - ×Ô¶¯ÇŽӵ½H5
- */
- var LogManagerH5 = {
- logLevel: 'DEBUG',
- enablePrefix: true,
- bridge: null,
-
- ICONS: {
- DEBUG: '[D]',
- INFO: '[I]',
- WARN: '[W]',
- ERROR: '[E]',
- SUCCESS: '[S]',
- GOLD: '[G]'
- },
-
- /**
- * ³õʼ»¯²¢°ó¶¨µ½WebView
- * @param {WebView} webView - H5µÄWebViewʵÀý
- * @param {Activity} activity - »î¶¯ÊµÀý
- */
- init: function(webView, activity) {
- this.bridge = LogBridge;
- this.bridge.init(webView, activity);
- this.info('ÈÕ־ϵͳÒÑÁ¬½Óµ½H5½çÃæ');
- return this;
- },
-
- _formatMessage: function(message, icon) {
- if (this.enablePrefix && icon) {
- return icon + ' ' + message;
- }
- return message;
- },
-
- debug: function(message) {
- if (this.bridge) {
- this.bridge.log(this._formatMessage(message, this.ICONS.DEBUG), 'debug');
- }
- printl(message);
- },
-
- info: function(message) {
- if (this.bridge) {
- this.bridge.log(this._formatMessage(message, this.ICONS.INFO), 'info');
- }
- printl(message);
- },
-
- warn: function(message) {
- if (this.bridge) {
- this.bridge.log(this._formatMessage(message, this.ICONS.WARN), 'warn');
- }
- printl(message);
- },
-
- error: function(message) {
- if (this.bridge) {
- this.bridge.log(this._formatMessage(message, this.ICONS.ERROR), 'error');
- }
- printl(message);
- },
-
- success: function(message) {
- if (this.bridge) {
- this.bridge.log(this._formatMessage(message, this.ICONS.SUCCESS), 'success');
- }
- printl(message);
- },
-
- gold: function(message) {
- if (this.bridge) {
- this.bridge.log(this._formatMessage(message, this.ICONS.GOLD), 'gold');
- }
- printl(message);
- }
- };
- /**
- * ʹÓÃʾÀý:
- *
- * // ÔÚÄãµÄÖ÷½Å±¾ÖÐ:
- * Import("LogBridge.js");
- *
- * var web = new WebView();
- * web.show();
- * web.loadFile('h5.html');
- *
- * // ³õʼ»¯ÈÕÖ¾ÇÅ½Ó (»á×èÈûµÈ´ýWebView¼ÓÔØ)
- * LogManagerH5.init(web);
- *
- * // ÏÖÔÚËùÓÐÈÕÖ¾¶¼»áÊä³öµ½H5½çÃæ
- * LogManagerH5.info('ϵͳÆô¶¯');
- * LogManagerH5.success('ÈÎÎñÍê³É');
- * LogManagerH5.gold('»ñµÃ100½ð±Ò');
- */
- /**
- * ============================================
- * 🍎 printÈÕ־ϵͳÍêÕûʾÀý
- * ¹¦ÄÜ: ÈÕÖ¾¼Ç¼¡¢µ÷ÊÔÊä³ö¡¢»Öл¹ÜÀí
- * ½»Á÷QQȺ: 711841924 (Ⱥһ) / 528816639 (Æ»¹ûÄÚ²âȺ)
- * ============================================
- */
¸´ÖÆ´úÂë
|
|