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

feat: ios safari extension POC #3

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions cookie-slayer/cookie-slayer 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": "cookie-slayer Extension",
"description": "The display name for the extension."
},
"extension_description": {
"message": "This is cookie-slayer Extension. You should tell us what your extension does here.",
"description": "Description of what the extension does."
}
}
6 changes: 6 additions & 0 deletions cookie-slayer/cookie-slayer Extension/Resources/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
// console.log("Received request: ", request);
//
// if (request.greeting === "hello")
// sendResponse({ farewell: "goodbye" });
//});
71 changes: 71 additions & 0 deletions cookie-slayer/cookie-slayer Extension/Resources/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//browser.runtime.sendMessage({ greeting: "hello" }).then((response) => {
// console.log("Received response: ", response);
//});
//
//browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
// console.log("Received request: ", request);
//});

const cookieRequestBannerSelectors = [
'#onetrust-consent-sdk', // onetrust
'#cookie-popup-with-overlay',
'#cookie-information-template-wrapper',
'.cky-consent-container', // cookieyes
'.iubenda-cs-container', // iubenda
'#termly-code-snippet-support', // termly
'.osano-cm-dialog', // osano
'.frame-content__inner', //securityprivacy.ai
'#CybotCookiebotDialog', // goodtimes.io
];

const waitForElm = (selector) => {
return new Promise(resolve => {
// if the selector takes longer than 15 seconds to load, we will just assume it will never load
const timeout = setTimeout(() => {
resolve(null);
}, 15000);
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}

const observer = new MutationObserver(_mutations => {
if (document.querySelector(selector)) {
observer.disconnect();
clearTimeout(timeout);
console.log('found selector', selector);
resolve(document.querySelector(selector));
}
});

// If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}

const hideSelector = async (selector) => {
const bannerEle = await waitForElm(selector);
if (bannerEle) {
bannerEle.remove();
}
}

const hideAll = () => {
for (const selector of cookieRequestBannerSelectors) {
// we don't want to wait for each selector since its possible that the
// selector will never be present on the page
void hideSelector(selector);
}
}

/**
* Check and set a global guard variable.
* If this content script is injected into the same page again,
* it will do nothing next time.
*/
if (!window.hasRunCookieKillerContentScript) {
window.hasRunCookieKillerContentScript = true;
hideAll()
}
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions cookie-slayer/cookie-slayer Extension/Resources/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"manifest_version": 3,
"default_locale": "en",

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

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

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

"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/toolbar-icon-16.png",
"19": "images/toolbar-icon-19.png",
"32": "images/toolbar-icon-32.png",
"38": "images/toolbar-icon-38.png",
"48": "images/toolbar-icon-48.png",
"72": "images/toolbar-icon-72.png"
}
},

"permissions": ["<all_urls>"]
}
15 changes: 15 additions & 0 deletions cookie-slayer/cookie-slayer Extension/Resources/popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
:root {
color-scheme: light dark;
}

body {
width: 100px;
padding: 10px;

font-family: system-ui;
text-align: center;
}

@media (prefers-color-scheme: dark) {
/* Dark Mode styles go here. */
}
11 changes: 11 additions & 0 deletions cookie-slayer/cookie-slayer Extension/Resources/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="popup.css">
<script type="module" src="popup.js"></script>
</head>
<body>
<strong>Hello World!</strong>
</body>
</html>
1 change: 1 addition & 0 deletions cookie-slayer/cookie-slayer Extension/Resources/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World!", browser);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SafariWebExtensionHandler.swift
// cookie-slayer Extension
//
// Created by katie on 5/21/24.
//

import SafariServices
import os.log

class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {

func beginRequest(with context: NSExtensionContext) {
let request = context.inputItems.first as? NSExtensionItem

let profile: UUID?
if #available(iOS 17.0, macOS 14.0, *) {
profile = request?.userInfo?[SFExtensionProfileKey] as? UUID
} else {
profile = request?.userInfo?["profile"] as? UUID
}

let message: Any?
if #available(iOS 17.0, macOS 14.0, *) {
message = request?.userInfo?[SFExtensionMessageKey]
} else {
message = request?.userInfo?["message"]
}

os_log(.default, "Received message from browser.runtime.sendNativeMessage: %@ (profile: %@)", String(describing: message), profile?.uuidString ?? "none")

let response = NSExtensionItem()
response.userInfo = [ SFExtensionMessageKey: [ "echo": message ] ]

context.completeRequest(returningItems: [ response ], completionHandler: nil)
}

}
Loading
Loading