import WebKit
import JavaScriptCore
-fileprivate extension Selector {
-
- static let reloadContent = #selector(GameViewController.reloadContent(_:))
- static let deleteCacheAndReload = #selector(GameViewController.deleteCacheAndReload(_:))
- static let screenShot = #selector(GameViewController.screenShot(_:))
-}
-
final class GameViewController: NSViewController {
private static let gamePageURL = "http://www.dmm.com/netgame/social/-/gadgets/=/app_id=854854/"
@IBOutlet var webView: WebView!
- override var nibName: String! {
+ override var nibName: NSNib.Name {
- return "GameViewController"
+ return .nibName(instanceOf: self)
}
- fileprivate var flashTopLeft = NSPoint(x: 2600, y: 1445)
+ private var flashTopLeft = NSPoint(x: 2600, y: 1445)
private var clipView: NSClipView {
return view as! NSClipView // swiftlint:disable:this force_cast
@IBAction func reloadContent(_ sender: AnyObject?) {
- guard let _ = webView.mainFrameURL
- else {
- webView.mainFrameURL = GameViewController.gamePageURL
- return
+ guard let _ = webView.mainFrameURL else {
+
+ webView.mainFrameURL = GameViewController.gamePageURL
+ return
}
// ゲームページでない場合はゲームページを表示する
let untilDate = prevDate.addingTimeInterval(1 * 60)
let date = DateFormatter.localizedString(from: untilDate, dateStyle: .none, timeStyle: .medium)
let alert = NSAlert()
- alert.messageText = NSLocalizedString("Reload interval is too short?", comment: "")
- let format = NSLocalizedString("Reload interval is too short.\nWait until %@.", comment: "")
+ alert.messageText = LocalizedStrings.reloadTimeShortenMessage.string
+ let format = LocalizedStrings.reloadTimeShortenInfo.string
alert.informativeText = String(format: format, date)
alert.runModal()
let panel = ProgressPanel()
- guard let window = view.window,
- let panelWindow = panel.window
- else { return }
+ guard let window = view.window else { return }
+ guard let panelWindow = panel.window else { return }
panel.title = ""
- panel.message = NSLocalizedString("Deleting caches...", comment: "Deleting caches...")
+ panel.message = LocalizedStrings.deletingCacheInfo.string
panel.animate = true
- window.beginSheet(panelWindow) { _ in NSSound(named: "Submarine")?.play() }
+ window.beginSheet(panelWindow) { _ in NSSound(named: NSSound.Name("Submarine"))?.play() }
AppDelegate.shared.clearCache()
window.endSheet(panelWindow)
}
- @IBAction func screenShot(_ sender: AnyObject?) {
+ func screenshotOld() {
let frame = webView.visibleRect
let screenshotBorder = UserDefaults.standard[.screenShotBorderWidth]
AppDelegate.shared.registerScreenshot(rep, fromOnScreen: .zero)
}
+ @available(OSX 10.13, *)
+ func screenshot() {
+
+ let frame = view.visibleRect
+ let screenshotBorder = UserDefaults.standard[.screenShotBorderWidth]
+ let f = frame.insetBy(dx: -screenshotBorder, dy: -screenshotBorder)
+ let windowCoordinateFrame = view.convert(f, to: nil)
+
+ guard let window = view.window else { return Logger.shared.log("Can not get window") }
+ let screenCoordinsteFrame = window.convertToScreen(windowCoordinateFrame)
+
+ guard let screen = NSScreen.main else { return Logger.shared.log("Can not get Screen") }
+ let scFrame = screen.frame
+
+ guard let cxt = window.graphicsContext?.cgContext else { return Logger.shared.log("Cannot get Context") }
+ let deviceCoordinateFrame = cxt.convertToDeviceSpace(screenCoordinsteFrame)
+ let raio = deviceCoordinateFrame.size.width / screenCoordinsteFrame.size.width
+
+ let trimRect = CGRect(x: raio * screenCoordinsteFrame.origin.x,
+ y: raio * (scFrame.size.height - screenCoordinsteFrame.origin.y - screenCoordinsteFrame.size.height),
+ width: raio * screenCoordinsteFrame.size.width,
+ height: raio * screenCoordinsteFrame.size.height)
+
+ guard let fullSizeImage = CGDisplayCreateImage(CGMainDisplayID()) else { return Logger.shared.log("Can not get Image") }
+
+ guard let image = fullSizeImage.cropping(to: trimRect) else { return Logger.shared.log("Can not trim image") }
+
+ let rep = NSBitmapImageRep(cgImage: image)
+
+ AppDelegate.shared.registerScreenshot(rep, fromOnScreen: .zero)
+ }
+
+ @IBAction func screenShot(_ sender: AnyObject?) {
+
+ if #available(OSX 10.13, *) {
+
+ screenshot()
+
+ } else {
+
+ screenshotOld()
+ }
+ }
+
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
- if menuItem.action == .reloadContent {
+ guard let action: Selector = menuItem.action else { return false }
+
+ switch action {
- guard let _ = webView.mainFrame,
- let frameURL = webView.mainFrameURL
- else { return true }
+ case #selector(GameViewController.reloadContent(_:)):
+ guard let _ = webView.mainFrame else { return true }
+ guard let frameURL = webView.mainFrameURL else { return true }
switch frameURL {
case GameViewController.gamePageURL:
- menuItem.title = NSLocalizedString("Reload", comment: "Reload menu, reload")
+ menuItem.title = LocalizedStrings.reload.string
case let s where s.hasPrefix(GameViewController.loginPageURLPrefix):
- menuItem.title = NSLocalizedString("Reload", comment: "Reload menu, reload")
+ menuItem.title = LocalizedStrings.reload.string
default:
- menuItem.title = NSLocalizedString("Back To Game", comment: "Reload menu, back to game")
+ menuItem.title = LocalizedStrings.backToGame.string
}
return true
- }
-
- if menuItem.action == .deleteCacheAndReload {
+ case #selector(GameViewController.deleteCacheAndReload(_:)):
return true
- }
-
- if menuItem.action == .screenShot {
+ case #selector(GameViewController.screenShot(_:)):
return true
+
+ default: return false
}
-
- return false
}
}
func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) {
- guard let path = frame.dataSource?.initialRequest.url?.path
- else { return }
+ guard let path = frame.dataSource?.initialRequest.url?.path else { return }
let handler: (JSContext?, JSValue?) -> Void = { (_, exception) in
} else {
print("Caught exception in evaluteScript")
-
}
}
if path.hasSuffix("gadgets/ifr") {
- guard let context = frame.javaScriptContext
- else { return }
+ guard let context = frame.javaScriptContext else { return }
context.exceptionHandler = handler
context.evaluateScript(
if path.hasSuffix("app_id=854854") {
- guard let context = frame.javaScriptContext
- else { return }
+ guard let context = frame.javaScriptContext else { return }
context.exceptionHandler = handler
context.evaluateScript(
)
let validIframe = context.objectForKeyedSubscript("validIframe").toInt32()
- guard validIframe != 0
- else { return }
+ guard validIframe != 0 else { return }
let top = context.objectForKeyedSubscript("atop").toDouble()
let left = context.objectForKeyedSubscript("aleft").toDouble()
func webView(_ sender: WebView!, contextMenuItemsForElement element: [AnyHashable: Any]!, defaultMenuItems: [Any]!) -> [Any]! {
- guard let menuItems = defaultMenuItems as? [NSMenuItem]
- else { return [] }
+ guard let menuItems = defaultMenuItems as? [NSMenuItem] else { return [] }
return menuItems.filter { !GameViewController.excludeMenuItemTag.contains($0.tag) }
}