From 8a94a7e70a6c5d1dc8ef1d265f18f017c8c3eb13 Mon Sep 17 00:00:00 2001 From: mathieudutour Date: Sat, 20 Oct 2018 18:35:46 +0200 Subject: [PATCH] add insertJS method --- lib/index.js | 6 +++--- lib/set-delegates.js | 2 +- lib/webview-api.js | 20 ++++++++++++++++++-- package.json | 39 ++++++++++++++++++++++++++------------- remote.js | 8 +------- 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4f91cb6..83bb9b7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -233,9 +233,9 @@ function BrowserWindow(options) { if (options.acceptsFirstMouse) { browserWindow.on('focus', function(event) { if (event.type() === NSEventTypeLeftMouseDown) { - browserWindow.webContents.executeJavaScript( - dispatchFirstClick(webView, event) - ) + browserWindow.webContents + .executeJavaScript(dispatchFirstClick(webView, event)) + .catch(() => {}) } }) } diff --git a/lib/set-delegates.js b/lib/set-delegates.js index fe78de8..ed6b1ad 100644 --- a/lib/set-delegates.js +++ b/lib/set-delegates.js @@ -22,7 +22,7 @@ var WebScriptHandlerClass // - 'media-started-playing' // - 'media-paused' // - 'did-change-theme-color' -// - 'update-target-url' +// - 'update-target-url' // - 'cursor-changed' // - 'context-menu' // - 'select-bluetooth-device' diff --git a/lib/webview-api.js b/lib/webview-api.js index d40702c..a496c99 100644 --- a/lib/webview-api.js +++ b/lib/webview-api.js @@ -137,6 +137,17 @@ module.exports = function buildAPI(browserWindow, panel, webview) { .userContentController() .addUserScript(script) } + webContents.insertJS = function(source) { + var script = WKUserScript.alloc().initWithSource_injectionTime_forMainFrameOnly( + source, + 0, + true + ) + webview + .configuration() + .userContentController() + .addUserScript(script) + } webContents.executeJavaScript = function(script, userGesture, callback) { if (typeof userGesture === 'function') { callback = userGesture @@ -150,14 +161,19 @@ module.exports = function buildAPI(browserWindow, panel, webview) { result, err ) { + var isError = + err && + err.class && + (String(err.class()) === 'NSException' || + String(err.class()) === 'NSError') if (callback) { try { - callback(err, result) + callback(isError ? err : null, result) } catch (error) { // /shrug } resolve() - } else if (err && err.description) { + } else if (isError) { reject(err) } else { resolve(result) diff --git a/package.json b/package.json index 6a45708..f0b7647 100644 --- a/package.json +++ b/package.json @@ -7,38 +7,51 @@ "cocoascript-class": "*" }, "devDependencies": { - "eslint": "^4.19.1", - "eslint-config-airbnb-base": "^12.1.0", - "eslint-config-prettier": "^2.9.0", - "eslint-plugin-import": "^2.10.0", - "eslint-plugin-prettier": "^2.6.0", - "lint-staged": "^7.0.3", + "eslint": "^5.7.0", + "eslint-config-airbnb-base": "^13.1.0", + "eslint-config-prettier": "^3.1.0", + "eslint-plugin-import": "^2.14.0", + "eslint-plugin-prettier": "^3.0.0", + "lint-staged": "^7.3.0", "pre-commit": "^1.2.2", - "prettier": "^1.11.1" + "prettier": "^1.14.3" }, "scripts": { "test": "npm run lint", "lint": "find . -name \"*.js\" | grep -v -f .gitignore | xargs eslint", "prettier:base": "prettier --write", - "prettify": - "find . -name \"*.js\" | grep -v -f .gitignore | xargs npm run prettier:base", + "prettify": "find . -name \"*.js\" | grep -v -f .gitignore | xargs npm run prettier:base", "lint-staged": "lint-staged" }, "repository": { "type": "git", "url": "git+https://github.com/mathieudutour/sketch-module-web-view.git" }, - "keywords": ["sketch", "module", "webview", "ui"], + "keywords": [ + "sketch", + "module", + "webview", + "ui" + ], "author": "Mathieu Dutour (http://mathieu.dutour.me/)", "license": "MIT", "bugs": { "url": "https://github.com/mathieudutour/sketch-module-web-view/issues" }, "homepage": "https://github.com/mathieudutour/sketch-module-web-view#readme", - "pre-commit": ["lint-staged"], + "pre-commit": [ + "lint-staged" + ], "lint-staged": { - "*.{js,ts}": ["npm run prettier:base", "eslint", "git add"], - "*.{md}": ["npm run prettier:base", "git add"] + "*.{js,ts}": [ + "npm run prettier:base", + "eslint", + "git add" + ], + "*.{md}": [ + "npm run prettier:base", + "git add" + ] }, "prettier": { "proseWrap": "never", diff --git a/remote.js b/remote.js index 0c81c02..6633ab1 100644 --- a/remote.js +++ b/remote.js @@ -3,13 +3,7 @@ var BrowserWindow = require('./lib') var threadDictionary = NSThread.mainThread().threadDictionary() -module.exports.getWebview = function getWebview(identifier) { - var panel = threadDictionary[identifier] - if (!panel) { - return undefined - } - return BrowserWindow.fromPanel(panel, identifier) -} +module.exports.getWebview = BrowserWindow.fromId module.exports.isWebviewPresent = function isWebviewPresent(identifier) { return !!threadDictionary[identifier]