From 2006e3a4ea110e2245d953503c74cbc346cac602 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 8 Aug 2019 04:33:35 -0400 Subject: [PATCH] More experimentation - Click toolbar button - It injects an iframe that that loads from S3 with a JS file from S3 - That JS posts a message to the parent window - The injected script in the parent window sends a message to the background Swift code - The background code makes an HTTP request and sends the result back to the injected script - The injected script logs the response to the console --- .gitignore | 26 +++ ExtensionApp.xcodeproj/project.pbxproj | 199 +++++++++++++++++- .../xcschemes/xcschememanagement.plist | 5 + ExtensionApp/AppDelegate.swift | 3 +- ExtensionApp/Info.plist | 2 +- ExtensionApp/ViewController.swift | 3 +- SafariExtension/Info.plist | 8 +- SafariExtension/SafariExtension.entitlements | 12 +- SafariExtension/SafariExtensionHandler.swift | 10 +- .../SafariExtensionViewController.swift | 3 +- SafariExtension/script.js | 42 +++- 11 files changed, 284 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9105546 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## User settings +xcuserdata/ + +## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) +*.xcscmblueprint +*.xccheckout + +## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) +build/ +DerivedData/ +*.moved-aside +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 + +## Gcc Patch +/*.gcno diff --git a/ExtensionApp.xcodeproj/project.pbxproj b/ExtensionApp.xcodeproj/project.pbxproj index 4b75a0b..6cf5477 100644 --- a/ExtensionApp.xcodeproj/project.pbxproj +++ b/ExtensionApp.xcodeproj/project.pbxproj @@ -11,8 +11,39 @@ B39AD239229FD14D00F97FA9 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39AD238229FD14D00F97FA9 /* ViewController.swift */; }; B39AD23B229FD14E00F97FA9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B39AD23A229FD14E00F97FA9 /* Assets.xcassets */; }; B39AD23E229FD14E00F97FA9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B39AD23C229FD14E00F97FA9 /* Main.storyboard */; }; + B39AD24D229FD1FB00F97FA9 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B39AD24C229FD1FB00F97FA9 /* Cocoa.framework */; }; + B39AD250229FD1FB00F97FA9 /* SafariExtensionHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39AD24F229FD1FB00F97FA9 /* SafariExtensionHandler.swift */; }; + B39AD252229FD1FB00F97FA9 /* SafariExtensionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39AD251229FD1FB00F97FA9 /* SafariExtensionViewController.swift */; }; + B39AD255229FD1FB00F97FA9 /* SafariExtensionViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B39AD253229FD1FB00F97FA9 /* SafariExtensionViewController.xib */; }; + B39AD258229FD1FB00F97FA9 /* script.js in Resources */ = {isa = PBXBuildFile; fileRef = B39AD257229FD1FB00F97FA9 /* script.js */; }; + B39AD25A229FD1FB00F97FA9 /* ToolbarItemIcon.pdf in Resources */ = {isa = PBXBuildFile; fileRef = B39AD259229FD1FB00F97FA9 /* ToolbarItemIcon.pdf */; }; + B39AD25E229FD1FB00F97FA9 /* SafariExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = B39AD24A229FD1FA00F97FA9 /* SafariExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; /* End PBXBuildFile section */ +/* Begin PBXContainerItemProxy section */ + B39AD25C229FD1FB00F97FA9 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = B39AD22B229FD14D00F97FA9 /* Project object */; + proxyType = 1; + remoteGlobalIDString = B39AD249229FD1FA00F97FA9; + remoteInfo = SafariExtension; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXCopyFilesBuildPhase section */ + B39AD262229FD1FB00F97FA9 /* Embed App Extensions */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 13; + files = ( + B39AD25E229FD1FB00F97FA9 /* SafariExtension.appex in Embed App Extensions */, + ); + name = "Embed App Extensions"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + /* Begin PBXFileReference section */ B39AD233229FD14D00F97FA9 /* ExtensionApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExtensionApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; B39AD236229FD14D00F97FA9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -21,6 +52,15 @@ B39AD23D229FD14E00F97FA9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; B39AD23F229FD14E00F97FA9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; B39AD240229FD14E00F97FA9 /* ExtensionApp.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ExtensionApp.entitlements; sourceTree = ""; }; + B39AD24A229FD1FA00F97FA9 /* SafariExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = SafariExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; + B39AD24C229FD1FB00F97FA9 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; + B39AD24F229FD1FB00F97FA9 /* SafariExtensionHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariExtensionHandler.swift; sourceTree = ""; }; + B39AD251229FD1FB00F97FA9 /* SafariExtensionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariExtensionViewController.swift; sourceTree = ""; }; + B39AD254229FD1FB00F97FA9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/SafariExtensionViewController.xib; sourceTree = ""; }; + B39AD256229FD1FB00F97FA9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + B39AD257229FD1FB00F97FA9 /* script.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = script.js; sourceTree = ""; }; + B39AD259229FD1FB00F97FA9 /* ToolbarItemIcon.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = ToolbarItemIcon.pdf; sourceTree = ""; }; + B39AD25B229FD1FB00F97FA9 /* SafariExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SafariExtension.entitlements; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -31,6 +71,14 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + B39AD247229FD1FA00F97FA9 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + B39AD24D229FD1FB00F97FA9 /* Cocoa.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -38,6 +86,8 @@ isa = PBXGroup; children = ( B39AD235229FD14D00F97FA9 /* ExtensionApp */, + B39AD24E229FD1FB00F97FA9 /* SafariExtension */, + B39AD24B229FD1FB00F97FA9 /* Frameworks */, B39AD234229FD14D00F97FA9 /* Products */, ); sourceTree = ""; @@ -46,6 +96,7 @@ isa = PBXGroup; children = ( B39AD233229FD14D00F97FA9 /* ExtensionApp.app */, + B39AD24A229FD1FA00F97FA9 /* SafariExtension.appex */, ); name = Products; sourceTree = ""; @@ -63,6 +114,28 @@ path = ExtensionApp; sourceTree = ""; }; + B39AD24B229FD1FB00F97FA9 /* Frameworks */ = { + isa = PBXGroup; + children = ( + B39AD24C229FD1FB00F97FA9 /* Cocoa.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + B39AD24E229FD1FB00F97FA9 /* SafariExtension */ = { + isa = PBXGroup; + children = ( + B39AD24F229FD1FB00F97FA9 /* SafariExtensionHandler.swift */, + B39AD251229FD1FB00F97FA9 /* SafariExtensionViewController.swift */, + B39AD253229FD1FB00F97FA9 /* SafariExtensionViewController.xib */, + B39AD256229FD1FB00F97FA9 /* Info.plist */, + B39AD257229FD1FB00F97FA9 /* script.js */, + B39AD259229FD1FB00F97FA9 /* ToolbarItemIcon.pdf */, + B39AD25B229FD1FB00F97FA9 /* SafariExtension.entitlements */, + ); + path = SafariExtension; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -73,16 +146,35 @@ B39AD22F229FD14D00F97FA9 /* Sources */, B39AD230229FD14D00F97FA9 /* Frameworks */, B39AD231229FD14D00F97FA9 /* Resources */, + B39AD262229FD1FB00F97FA9 /* Embed App Extensions */, ); buildRules = ( ); dependencies = ( + B39AD25D229FD1FB00F97FA9 /* PBXTargetDependency */, ); name = ExtensionApp; productName = ExtensionApp; productReference = B39AD233229FD14D00F97FA9 /* ExtensionApp.app */; productType = "com.apple.product-type.application"; }; + B39AD249229FD1FA00F97FA9 /* SafariExtension */ = { + isa = PBXNativeTarget; + buildConfigurationList = B39AD25F229FD1FB00F97FA9 /* Build configuration list for PBXNativeTarget "SafariExtension" */; + buildPhases = ( + B39AD246229FD1FA00F97FA9 /* Sources */, + B39AD247229FD1FA00F97FA9 /* Frameworks */, + B39AD248229FD1FA00F97FA9 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SafariExtension; + productName = SafariExtension; + productReference = B39AD24A229FD1FA00F97FA9 /* SafariExtension.appex */; + productType = "com.apple.product-type.app-extension"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -91,11 +183,19 @@ attributes = { LastSwiftUpdateCheck = 1020; LastUpgradeCheck = 1020; - ORGANIZATIONNAME = "Michal Rentka"; + ORGANIZATIONNAME = "Corporation for Digital Scholarship"; TargetAttributes = { B39AD232229FD14D00F97FA9 = { CreatedOnToolsVersion = 10.2.1; }; + B39AD249229FD1FA00F97FA9 = { + CreatedOnToolsVersion = 10.2.1; + SystemCapabilities = { + com.apple.Sandbox = { + enabled = 1; + }; + }; + }; }; }; buildConfigurationList = B39AD22E229FD14D00F97FA9 /* Build configuration list for PBXProject "ExtensionApp" */; @@ -112,6 +212,7 @@ projectRoot = ""; targets = ( B39AD232229FD14D00F97FA9 /* ExtensionApp */, + B39AD249229FD1FA00F97FA9 /* SafariExtension */, ); }; /* End PBXProject section */ @@ -126,6 +227,16 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + B39AD248229FD1FA00F97FA9 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B39AD25A229FD1FB00F97FA9 /* ToolbarItemIcon.pdf in Resources */, + B39AD255229FD1FB00F97FA9 /* SafariExtensionViewController.xib in Resources */, + B39AD258229FD1FB00F97FA9 /* script.js in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -138,8 +249,25 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + B39AD246229FD1FA00F97FA9 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B39AD252229FD1FB00F97FA9 /* SafariExtensionViewController.swift in Sources */, + B39AD250229FD1FB00F97FA9 /* SafariExtensionHandler.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ +/* Begin PBXTargetDependency section */ + B39AD25D229FD1FB00F97FA9 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = B39AD249229FD1FA00F97FA9 /* SafariExtension */; + targetProxy = B39AD25C229FD1FB00F97FA9 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + /* Begin PBXVariantGroup section */ B39AD23C229FD14E00F97FA9 /* Main.storyboard */ = { isa = PBXVariantGroup; @@ -149,6 +277,14 @@ name = Main.storyboard; sourceTree = ""; }; + B39AD253229FD1FB00F97FA9 /* SafariExtensionViewController.xib */ = { + isa = PBXVariantGroup; + children = ( + B39AD254229FD1FB00F97FA9 /* Base */, + ); + name = SafariExtensionViewController.xib; + sourceTree = ""; + }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ @@ -270,18 +406,21 @@ B39AD244229FD14E00F97FA9 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = ExtensionApp/ExtensionApp.entitlements; + CODE_SIGN_IDENTITY = "Mac Developer"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - DEVELOPMENT_TEAM = 8AQ7YSWBDJ; + DEVELOPMENT_TEAM = 2NXF962MMK; INFOPLIST_FILE = ExtensionApp/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.michalrentka.ExtensionApp; + PRODUCT_BUNDLE_IDENTIFIER = org.zotero.SafariExtensionApp; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; }; name = Debug; @@ -289,18 +428,59 @@ B39AD245229FD14E00F97FA9 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = ExtensionApp/ExtensionApp.entitlements; + CODE_SIGN_IDENTITY = "Mac Developer"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - DEVELOPMENT_TEAM = 8AQ7YSWBDJ; + DEVELOPMENT_TEAM = 2NXF962MMK; INFOPLIST_FILE = ExtensionApp/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.michalrentka.ExtensionApp; + PRODUCT_BUNDLE_IDENTIFIER = org.zotero.SafariExtensionApp; + PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + B39AD260229FD1FB00F97FA9 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_ENTITLEMENTS = SafariExtension/SafariExtension.entitlements; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 2NXF962MMK; + INFOPLIST_FILE = SafariExtension/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@executable_path/../../../../Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = org.zotero.SafariExtensionApp.SafariExtension; PRODUCT_NAME = "$(TARGET_NAME)"; + SKIP_INSTALL = YES; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + B39AD261229FD1FB00F97FA9 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_ENTITLEMENTS = SafariExtension/SafariExtension.entitlements; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 2NXF962MMK; + INFOPLIST_FILE = SafariExtension/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@executable_path/../../../../Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = org.zotero.SafariExtensionApp.SafariExtension; + PRODUCT_NAME = "$(TARGET_NAME)"; + SKIP_INSTALL = YES; SWIFT_VERSION = 5.0; }; name = Release; @@ -326,6 +506,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + B39AD25F229FD1FB00F97FA9 /* Build configuration list for PBXNativeTarget "SafariExtension" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B39AD260229FD1FB00F97FA9 /* Debug */, + B39AD261229FD1FB00F97FA9 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = B39AD22B229FD14D00F97FA9 /* Project object */; diff --git a/ExtensionApp.xcodeproj/xcuserdata/michalrentka.xcuserdatad/xcschemes/xcschememanagement.plist b/ExtensionApp.xcodeproj/xcuserdata/michalrentka.xcuserdatad/xcschemes/xcschememanagement.plist index d5dbea4..3794659 100644 --- a/ExtensionApp.xcodeproj/xcuserdata/michalrentka.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/ExtensionApp.xcodeproj/xcuserdata/michalrentka.xcuserdatad/xcschemes/xcschememanagement.plist @@ -9,6 +9,11 @@ orderHint 0 + SafariExtension.xcscheme_^#shared#^_ + + orderHint + 1 + diff --git a/ExtensionApp/AppDelegate.swift b/ExtensionApp/AppDelegate.swift index 7e061a1..c2ddd06 100644 --- a/ExtensionApp/AppDelegate.swift +++ b/ExtensionApp/AppDelegate.swift @@ -2,8 +2,7 @@ // AppDelegate.swift // ExtensionApp // -// Created by Michal Rentka on 30/05/2019. -// Copyright © 2019 Michal Rentka. All rights reserved. +// Copyright © 2019 Corporation for Digital Scholarship // import Cocoa diff --git a/ExtensionApp/Info.plist b/ExtensionApp/Info.plist index 2300336..d221eec 100644 --- a/ExtensionApp/Info.plist +++ b/ExtensionApp/Info.plist @@ -23,7 +23,7 @@ LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright - Copyright © 2019 Michal Rentka. All rights reserved. + Copyright © 2019 Corporation for Digital Scholarship NSMainStoryboardFile Main NSPrincipalClass diff --git a/ExtensionApp/ViewController.swift b/ExtensionApp/ViewController.swift index de7f629..2f9e493 100644 --- a/ExtensionApp/ViewController.swift +++ b/ExtensionApp/ViewController.swift @@ -2,8 +2,7 @@ // ViewController.swift // ExtensionApp // -// Created by Michal Rentka on 30/05/2019. -// Copyright © 2019 Michal Rentka. All rights reserved. +// Copyright © 2019 Corporation for Digital Scholarship // import Cocoa diff --git a/SafariExtension/Info.plist b/SafariExtension/Info.plist index 41b9edb..7b14844 100644 --- a/SafariExtension/Info.plist +++ b/SafariExtension/Info.plist @@ -48,16 +48,12 @@ SFSafariWebsiteAccess - Allowed Domains - - *.webkit.org - Level - Some + All NSHumanReadableCopyright - Copyright © 2019 Michal Rentka. All rights reserved. + Copyright © 2019 Corporation for Digital Scholarship NSHumanReadableDescription This is a Safari Extension. You should tell us what your extension does here. diff --git a/SafariExtension/SafariExtension.entitlements b/SafariExtension/SafariExtension.entitlements index f2ef3ae..40b639e 100644 --- a/SafariExtension/SafariExtension.entitlements +++ b/SafariExtension/SafariExtension.entitlements @@ -2,9 +2,13 @@ - com.apple.security.app-sandbox - - com.apple.security.files.user-selected.read-only - + com.apple.security.app-sandbox + + com.apple.security.files.user-selected.read-only + + com.apple.security.network.client + + com.apple.security.network.server + diff --git a/SafariExtension/SafariExtensionHandler.swift b/SafariExtension/SafariExtensionHandler.swift index da54530..bd0e9d8 100644 --- a/SafariExtension/SafariExtensionHandler.swift +++ b/SafariExtension/SafariExtensionHandler.swift @@ -2,8 +2,7 @@ // SafariExtensionHandler.swift // SafariExtension // -// Created by Michal Rentka on 30/05/2019. -// Copyright © 2019 Michal Rentka. All rights reserved. +// Copyright © 2019 Corporation for Digital Scholarship // import SafariServices @@ -45,8 +44,11 @@ class SafariExtensionHandler: SFSafariExtensionHandler { } override func toolbarItemClicked(in window: SFSafariWindow) { - // This method will be called when your toolbar item is clicked. - NSLog("The extension's toolbar item was clicked") + window.getActiveTab { (activeTab) in + activeTab?.getActivePage { (activePage) in + activePage?.dispatchMessageToScript(withName: "translate", userInfo: nil) + } + } } override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) { diff --git a/SafariExtension/SafariExtensionViewController.swift b/SafariExtension/SafariExtensionViewController.swift index b9e271d..f5b7850 100644 --- a/SafariExtension/SafariExtensionViewController.swift +++ b/SafariExtension/SafariExtensionViewController.swift @@ -2,8 +2,7 @@ // SafariExtensionViewController.swift // SafariExtension // -// Created by Michal Rentka on 30/05/2019. -// Copyright © 2019 Michal Rentka. All rights reserved. +// Copyright © 2019 Corporation for Digital Scholarship // import SafariServices diff --git a/SafariExtension/script.js b/SafariExtension/script.js index 34f7e35..388cb3e 100644 --- a/SafariExtension/script.js +++ b/SafariExtension/script.js @@ -1,3 +1,39 @@ -document.addEventListener("DOMContentLoaded", function(event) { - safari.extension.dispatchMessage("Hello World!"); -}); +// Inject the iframe into the top frame +if (window.top === window) { + safari.self.addEventListener("message", handleMessage); + + function handleMessage(event) { + if (event.name == 'translate') { + console.log("Injecting iframe"); + injectIframe(); + } + else if (event.name == 'loadedData') { + console.log("Got loaded data"); + console.log(event.message.title); + } + } + + function injectIframe() { + var iframe = document.getElementById("zotero-iframe"); + if (iframe) { + iframe.contentWindow.postMessage(['progressWindow.reopen', null], '*'); + return; + }; + + iframe = document.createElement("iframe"); + iframe.id = "zotero-iframe" + iframe.style.display = "none"; + iframe.style.borderStyle = "none"; + iframe.setAttribute("frameborder", "0"); + iframe.src = 'https://zotero-temp.s3.amazonaws.com/bookmarklet/safari-test/iframe.html'; + document.body.appendChild(iframe); + } + + document.addEventListener("DOMContentLoaded", function (event) { + window.addEventListener('message', function (event) { + if (event.origin == 'https://zotero-temp.s3.amazonaws.com' && event.data == 'Hello') { + safari.extension.dispatchMessage("loadData"); + } + }); + }); +}