Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser extensions #185

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions Finicky/Finicky/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
LSSetDefaultHandlerForURLScheme("http" as CFString, bundleId as CFString)
LSSetDefaultHandlerForURLScheme("https" as CFString, bundleId as CFString)
LSSetDefaultHandlerForURLScheme("finicky" as CFString, bundleId as CFString)
LSSetDefaultHandlerForURLScheme("finickys" as CFString, bundleId as CFString)
}

@IBAction func reloadConfig(_: NSMenuItem? = nil) {
Expand Down Expand Up @@ -231,17 +230,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}

@IBAction func testUrl(_ sender: NSTextField) {
guard var value = sender.stringValue.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
guard let value = sender.stringValue.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
logToConsole("Could not parse URL")
return
}

if value.starts(with: "finickys://") || value.starts(with: "finicky://") {
logToConsole("Finicky will convert finickys:// and finicky:// urls to https:// and http:// respectively")
value = value.replacingOccurrences(of: "finicky://", with: "http://", options: .literal, range: nil)
value = value.replacingOccurrences(of: "finickys://", with: "https://", options: .literal, range: nil)
}

if !value.starts(with: "https://"), !value.starts(with: "http://") {
logToConsole("Finicky only understands https:// and http:// urls")
return
Expand Down Expand Up @@ -299,21 +292,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
let pid = event!.attributeDescriptor(forKeyword: AEKeyword(keySenderPIDAttr))!.int32Value

let opener = Application(pid: pid)
var url = URL(string: event!.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))!.stringValue!)!

if url.scheme == "finicky" || url.scheme == "finickys" {
if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) {
if url.scheme == "finicky" {
urlComponents.scheme = "http"
}

if url.scheme == "finickys" {
urlComponents.scheme = "https"
}

url = urlComponents.url!
}

var rawUrl = event!.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))!.stringValue!

if rawUrl.hasPrefix("finicky://") {
rawUrl.removeFirst("finicky://".count)
}

let url = URL(string: rawUrl)!

shortUrlResolver.resolveUrl(url, callback: { (URL) -> Void in
self.callUrlHandlers(opener: opener, url: URL)
})
Expand Down
12 changes: 11 additions & 1 deletion Finicky/Finicky/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,19 @@
<string>https</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>FinickyHttpRewrite</string>
<key>CFBundleURLSchemes</key>
<array>
<string>finicky</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>310</string>
<string>311</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
20 changes: 20 additions & 0 deletions browser-extension/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const menuItemId = "finicky-open-url";

let browser = typeof window !== "undefined" && typeof window.browser !== "undefined" ? window.browser : chrome;


browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId !== menuItemId) {
return;
}

browser.tabs.update(tab.id, { url: "finicky://" + info.linkUrl });
});

try {
chrome.contextMenus.create({
id: menuItemId,
title: "Open with Finicky",
contexts: ["link"],
});
} catch (ex) {}
71 changes: 71 additions & 0 deletions browser-extension/contentScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let isIntercepting = false;

window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "alt") {
isIntercepting = true;
}
});

window.addEventListener("keyup", (event) => {
if (event.key.toLowerCase() === "alt") {
isIntercepting = false;
}
});

window.addEventListener("blur", (event) => {
isIntercepting = false;
});

function getAnchor(element) {
do {
if (element.tagName?.toLowerCase() === "a") {
return element;
}

element = element.parentNode;
} while (element.parentNode);

return undefined;
}

window.addEventListener(
"mousedown",
function (event) {
const anchor = getAnchor(event.target);
if (!anchor) return;

console.log("mousedowned something", anchor, isIntercepting);
}
);

window.addEventListener(
"click",
function (event) {
const anchor = capture(event);
if (!anchor) return;

console.log("clicked something", anchor, isIntercepting);

try {
const url = new URL(anchor.href, document.baseURI).href;
console.log("opening url in finicky", url);
window.location = "finicky://" + url;
} catch (ex) {
console.error("Finicky Browser Extension Error", ex);
}
},
true
);

function capture(event) {
if (!isIntercepting) return;

const anchor = getAnchor(event.target);

if (!anchor?.hasAttribute("href")) return;

event.preventDefault();
event.stopImmediatePropagation();

return anchor;
}
Binary file added browser-extension/icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser-extension/icon16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added browser-extension/icon48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions browser-extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Finicky",
"description": "The Official Finicky Browser Extension. Requires Finicky (macOS only)",
"version": "0.1",
"manifest_version": 2,
"homepage_url": "https://github.com/johnste/finicky",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["contentScript.js"]
}
],
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"permissions": ["contextMenus", "<all_urls>"]
}