Skip to content

Commit

Permalink
add insertJS method
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour committed Oct 20, 2018
1 parent 2f75661 commit 8a94a7e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/index.js
Expand Up @@ -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(() => {})
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/set-delegates.js
Expand Up @@ -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'
Expand Down
20 changes: 18 additions & 2 deletions lib/webview-api.js
Expand Up @@ -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
Expand All @@ -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)
Expand Down
39 changes: 26 additions & 13 deletions package.json
Expand Up @@ -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 <mathieu@dutour.me> (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",
Expand Down
8 changes: 1 addition & 7 deletions remote.js
Expand Up @@ -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]
Expand Down

0 comments on commit 8a94a7e

Please sign in to comment.