Skip to content

Commit

Permalink
user agent change implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalvinci committed Apr 5, 2022
1 parent 5e789fa commit 40f0de5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 73 deletions.
11 changes: 1 addition & 10 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
// background.js

let color = '#3aa757';

chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ color });
console.log('Default background color set to %cgreen', `color: ${color}`);
});


chrome.storage.sync.set({ UA_CHANGED: false });
13 changes: 0 additions & 13 deletions button.css
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
button {
height: 30px;
width: 30px;
outline: none;
margin: 10px;
border: none;
border-radius: 2px;
}

button.current {
box-shadow: 0 0 0 2px white,
0 0 0 4px black;
}
46 changes: 23 additions & 23 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "Getting Started Example",
"description": "Build an Extension!",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": ["storage", "activeTab", "scripting"],
"action": {
"default_popup": "popUp.html"
},
"default_icon": {
"16": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started16.png",
"32": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started32.png",
"48": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started48.png",
"128":"/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started128.png"
},
"default_icon": {
"16": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started16.png",
"32": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started32.png",
"48": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started48.png",
"128": "/home/akash/Documents/repos/browserFingerPrintSpoofing/images/get_started128.png"
}
"name": "Getting Started Example",
"description": "Build an Extension!",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": [
"scripting",
"storage",
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess"
],
"host_permissions": ["<all_urls>"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "./images/get_started16.png",
"32": "./images/get_started32.png",
"48": "./images/get_started48.png",
"128": "./images/get_started128.png"
}
}
}
10 changes: 0 additions & 10 deletions popUp.html

This file was deleted.

10 changes: 10 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="button.css" />
</head>
<body>
<button id="change-ua"></button>
<script src="popup.js"></script>
</body>
</html>
74 changes: 57 additions & 17 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,63 @@
// Initialize button with user's preferred color
let changeColor = document.getElementById("changeColor");
actionBtn = document.querySelector("#change-ua");

chrome.storage.sync.get("color", ({ color }) => {
changeColor.style.backgroundColor = color;
chrome.storage.sync.get("UA_CHANGED", ({ UA_CHANGED }) => {
actionBtn.textContent = UA_CHANGED ? "Stop" : "Change UA";
});
// When the button is clicked, inject setPageBackgroundColor into current page
changeColor.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

chrome.scripting.executeScript({
target: { tabId: tab.id },
function: setPageBackgroundColor,
});
actionBtn.addEventListener("click", (event) => {
chrome.storage.sync.get("UA_CHANGED", ({ UA_CHANGED }) => {
userAgentChanged = UA_CHANGED;
const newValue = !UA_CHANGED;
chrome.storage.sync.set({ UA_CHANGED: newValue }, async () => {
let userAgent = "";
if (!newValue) {
userAgent = window.navigator.userAgent;
actionBtn.textContent = "Change UA";
} else {
userAgent = await getRandomUserAgent();
actionBtn.textContent = "Stop";
}
chrome.declarativeNetRequest.updateDynamicRules(
{
removeRuleIds: [1],
addRules: [
{
id: 1,
priority: 10,
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "user-agent",
operation: "set",
value: userAgent,
},
],
},
condition: {
urlFilter: "*",
resourceTypes: ["main_frame"],
},
},
],
},
() => {
reload();
}
);
});
});
});

// The body of this function will be executed as a content script inside the
// current page
function setPageBackgroundColor() {
chrome.storage.sync.get("color", ({ color }) => {
document.body.style.backgroundColor = color;
});
async function reload() {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.tabs.reload(tab.id);
}

async function getRandomUserAgent() {
const response = await fetch(
"https://browser-fingerprint-spoof.herokuapp.com/getRandomUserAgent"
);
let userAgent = await response.text();
return userAgent;
}

0 comments on commit 40f0de5

Please sign in to comment.