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: Extension PoCs #303

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions extension/nfa-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NFA Extension (WIP - Testing Purposes)

This simple browser extension compares any active tab's URL against the NFA project's registry of domains. If there is a match, recommends the user to open the page via the direct IPNS/IPFS hash. Uses the default browser/metamask gateway to open the ipfs page, if the user accepts.
Binary file added extension/nfa-extension/images/icon-128.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 extension/nfa-extension/images/icon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions extension/nfa-extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Non-Fungible Apps",
"version": "1.0",
"description": "WIP!",
"permissions": ["notifications", "activeTab", "scripting", "storage", "tabs"],
"options_page": "options.html",
"background": {
"service_worker": "service-worker.js",
"type": "module"
},
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/icon-16.png",
"128": "images/icon-128.png"
}
},
"icons": {
"16": "images/icon-16.png",
"128": "images/icon-128.png"
},
"manifest_version": 3
}
20 changes: 20 additions & 0 deletions extension/nfa-extension/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<style>
button {
height: 30px;
width: 30px;
outline: none;
margin: 10px;
}
</style>
</head>
<body>
<div id="buttonDiv"></div>
<div>
<p>Choose a different background color!</p>
</div>
<script src="options.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions extension/nfa-extension/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const buttonDiv = document.getElementById('buttonDiv');
const buttonColors = ['#3aa757', '#e8453c', '#f9bb2d', '#4688f1'];

const createColorButtons = (buttonColors) => {
buttonColors.forEach((color) => {
const button = document.createElement('button');
button.style.backgroundColor = color;

button.addEventListener('click', () => {
chrome.storage.sync.set({ color }, () => {
console.log(`color is ${color}`);
});
});

buttonDiv.appendChild(button);
});
};

createColorButtons(buttonColors);
19 changes: 19 additions & 0 deletions extension/nfa-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "nfa-extension",
"version": "1.0.0",
"description": "This simple browser extension compares any active tab's URL against the NFA project's registry of domains. If there is a match, recommends the user to open the page via the direct IPNS/IPFS hash. Uses the default browser/metamask gateway to open the ipfs page, if the user accepts.",
"main": "options.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/EmperorOrokuSaki/nfa-extension.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/EmperorOrokuSaki/nfa-extension/issues"
},
"homepage": "https://github.com/EmperorOrokuSaki/nfa-extension#readme"
}
17 changes: 17 additions & 0 deletions extension/nfa-extension/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<style>
button {
height: 30px;
width: 30px;
outline: none;
background-color: #3aa757;
}
</style>
</head>
<body>
<button id="changeColor"></button>
<script src="popup.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions extension/nfa-extension/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const changeColorButton = document.getElementById('changeColor');

// Retrieve the color from storage and update the button's style and value
chrome.storage.sync.get('color', ({ color }) => {
changeColorButton.style.backgroundColor = color;
changeColorButton.setAttribute('value', color);
});

changeColorButton.addEventListener('click', (event) => {
const color = event.target.value;

// Query the active tab before injecting the content script
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
// Use the Scripting API to execute a script
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
args: [color],
func: setColor
});
});
});

function setColor(color) {
// There's a typo in the line below;
// ❌ colors should be ✅ color.
document.body.style.backgroundColor = color;
}
36 changes: 36 additions & 0 deletions extension/nfa-extension/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model builds {
id String @id @default(auto()) @map("_id") @db.ObjectId
commitHash String
domain String
githubRepository String
ipfsHash String
}

model tokens {
id String @id @default(auto()) @map("_id") @db.ObjectId
commitHash String
domain String
githubRepository String
ipfsHash String
owner String
tokenId Int
verified Boolean
}

model zones {
id String @id @default(auto()) @map("_id") @db.ObjectId
zoneId Int // The returned id from the creation call
name String // The assigned name at the time of creation
hostname String // The target domain that's assigned as hostname
sourceDomain String // The origin URL
}
72 changes: 72 additions & 0 deletions extension/nfa-extension/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//import { initPrisma, prisma } from './util/prismaHelper.js';

const mapping = {
'fleek.xyz': 'bafybeidtfkjek5emzjrzw6c4vtgwkfggfgpfdniamq4g4stqasknhxwpea',
'ipfs.tech': 'bafybeibwirqleiponhf7v76j7uwl2ffpw7tjebmbyhxbfkw3etj5a6okjm',
'docs.ipfs.tech': 'bafybeiebkh7kbsyofwlat7c6klayu57mmpy6362dso7egunokp7t3ary6u'
}

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
let url = new URL(tab.url);
if (changeInfo.status != "complete") return;
if (url.protocol == "ipfs://") return;

console.log(url.hostname);

if(mapping[url.hostname] !== undefined) {
chrome.notifications.create(mapping[url.hostname], {
type: 'basic',
iconUrl: './images/icon-16.png',
title: 'This domain is associated with an NFA.',
message: 'Click here to open the safe version!',
priority: 2,
});
}


// initPrisma();

// let urlLookup = prisma.tokens.findMany({
// where: {
// domain: url.hostname.replace('www', '')
// }
// });

// if (urlLookup.length > 0) {
// chrome.notifications.create('notificationID', {
// type: 'basic',
// iconUrl: './images/icon-16.png',
// title: 'This domain is associated with an NFA.',
// message: 'Do you want to open the safe version?',
// priority: 2,
// buttons: [{
// title: "Yes",
// // iconUrl: "./images/icon-16.png"
// }]
// });
// }
});

chrome.notifications.onClicked.addListener((notificationID) => {

chrome.tabs.create({
url: 'https://ipfs.io/ipfs/'+notificationID,
selected: true,
})

// if (notifId === myNotificationID) {
// if (btnIdx === 0) {
// window.open("...");
// } else if (btnIdx === 1) {
// saySorry();
// }
// }
});

// We may not need the `onActivated` listener as any URL change will trigger the `onUpdated` listener.
// chrome.tabs.onActivated.addListener(() => {
// chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
// var currentURL = tabs[0].url;
// console.log("onActivated:" + currentURL);
// })
// });
18 changes: 18 additions & 0 deletions extension/nfa-extension/util/prismaHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PrismaClient } from '@prisma/client';

export const prisma = new PrismaClient({log: ['warn', 'error']});

export async function initPrisma() {
// Connect the client
await prisma.$connect();
}

initPrisma()
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
})
.finally(() => {
prisma.$disconnect();
});
Loading