Skip to content

Commit

Permalink
Add Safari Extensions to repo (#56)
Browse files Browse the repository at this point in the history
* Add Safari Extensions to repo

* Delete .DS_Store that snuck in
  • Loading branch information
workwithnano authored Jan 2, 2024
1 parent 28828d0 commit b8baeed
Show file tree
Hide file tree
Showing 196 changed files with 7,819 additions and 0 deletions.
2 changes: 2 additions & 0 deletions safari/Legacy iOS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
*.xcuserdatad
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions safari/Legacy iOS/KagiForSafari Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.Safari.web-extension</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).SafariWebExtensionHandler</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extension_name": {
"message": "Kagi Search",
"description": "..."
},
"extension_description": {
"message": "With Kagi Search extension you can get your search queries sent to Kagi while using Safari.",
"description": "..."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
chrome.webNavigation.onBeforeNavigate.addListener(function(details) {
let url = details.url;
if (typeof(url) !== "undefined" && url.startsWith(`https://kagi.com`)) {
chrome.storage.local.set({ lastKagiUrl: url });
} else {
chrome.storage.local.set({ lastKagiUrl: "" });
}
});
113 changes: 113 additions & 0 deletions safari/Legacy iOS/KagiForSafari Extension/Resources/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const searchQuery = navigateIfNeeded(window.location);

if (window.location.href.startsWith("https://kagi.com/")) {
if (window.location.href.startsWith("https://kagi.com/signin") && getCookie("extRedirected") == "") {
setCookie("extRedirected", "1");
chrome.storage.local.get(["privateSessionLink", "lastKagiUrl"], function(value) {
var privateSessionLink = value.privateSessionLink;
var lastKagiUrl = value.lastKagiUrl;
var searchQuery = getParameterByName("q", lastKagiUrl)
if (typeof(privateSessionLink) !== "undefined" && privateSessionLink.startsWith(`https://kagi.com/`)) {
if (privateSessionLink.includes("q=%s")) {
window.location.replace(privateSessionLink.replace("%s", searchQuery != null ? searchQuery : ""));
} else {
window.location.replace(privateSessionLink.concat("&q=", searchQuery != null ? searchQuery : ""));
}
}
});
} else {
setCookie("extRedirected", "");
}
} else if (searchQuery != null) {
if (!shouldSkipRedirect()) {
window.location.replace(`https://kagi.com/search?q=${searchQuery}`);
}
}

function shouldSkipRedirect(url = window.location.href) {
var result = false;
const host = location.host.replace("www.", "");
if (host.includes("google.") && getParameterByName("client") == null) {
result = true;
} else if (host.includes("bing.") && getParameterByName("form") == null) {
result = true;
} else if (host.includes("duckduckgo.") && getParameterByName("t") == null) {
result = true;
} else if (host.includes("search.yahoo.") && getParameterByName("fr") == null) {
result = true;
}

return result;
}

function navigateIfNeeded(location) {
var result = null;
const host = location.host.replace("www.", "");

["google.", "bing.", "ecosia.", "search.yahoo"].forEach(function(item){
if (host.includes(item)) {
if (location.pathname === "/search") {
result = getParameterByName((host.includes("search.yahoo")) ? "p" : "q");
}
}
});


if (host.startsWith("duckduckgo.")) {
result = getParameterByName("q");
}

if (host.startsWith("yandex.")) {
if (location.pathname === "/search/touch/") {
result = getParameterByName("text");
}
}

["baidu.", "so."].forEach(function(item){
if (host.includes(item)) {
result = getParameterByName((host.includes("baidu.")) ? "oq" : "src");
}
});

if (host.startsWith("sogou.")) {
if (location.pathname === "/web") {
result = getParameterByName("query");
}
}

return result;
}

function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);

if (!results) return null;
if (!results[2]) return '';

return results[2].replace(/\+/g, ' ');
}

function setCookie(cname, cvalue) {
const d = new Date();
d.setTime(d.getTime() + (5000));
let expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions safari/Legacy iOS/KagiForSafari Extension/Resources/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"manifest_version": 2,
"default_locale": "en",

"name": "__MSG_extension_name__",
"description": "__MSG_extension_description__",
"version": "1.0.9",

"icons": {
"48": "images/icon.png",
"96": "images/icon.png",
"128": "images/icon.png"
},

"background": {
"scripts": ["background.js"],
"persistent": false
},

"content_scripts": [{
"js": [ "content.js" ],
"matches": [ "<all_urls>" ],
"run_at": "document_start"
}],

"browser_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/icon.png",
"19": "images/icon.png",
"32": "images/icon.png",
"48": "images/icon.png",
"72": "images/icon.png"
}
},

"permissions": [ "<all_urls>", "nativeMessaging", "storage", "webNavigation" ]
}
88 changes: 88 additions & 0 deletions safari/Legacy iOS/KagiForSafari Extension/Resources/popup.html

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions safari/Legacy iOS/KagiForSafari Extension/Resources/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
document.getElementById("footerLink").onclick = function() {
window.open("https://kagi.com/settings?p=user_details#sessionlink");
};

chrome.storage.local.get("privateSessionLink", function(value) {
var privateSessionLink = value.privateSessionLink;
if (typeof(privateSessionLink) !== "undefined") {
document.getElementById('privateSessionLink').value = value.privateSessionLink;
}
});

document.getElementById('privateSessionLink').addEventListener('input', function (evt) {
chrome.storage.local.set({ privateSessionLink: document.getElementById('privateSessionLink').value});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// SafariWebExtensionHandler.swift
// KagiForSafari Extension
//
// Created by apples on 31.01.2022.
//

import SafariServices
import os.log

class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
func beginRequest(with context: NSExtensionContext) {
let response = NSExtensionItem()
context.completeRequest(returningItems: [response], completionHandler: nil)
}
}
Loading

0 comments on commit b8baeed

Please sign in to comment.