From 40f0de5d6f8432ba5b7a2e18039927aa8c655196 Mon Sep 17 00:00:00 2001 From: Kalvinci Date: Tue, 5 Apr 2022 13:51:12 -0400 Subject: [PATCH] user agent change implemented --- background.js | 11 +------- button.css | 13 --------- manifest.json | 46 ++++++++++++++++---------------- popUp.html | 10 ------- popup.html | 10 +++++++ popup.js | 74 +++++++++++++++++++++++++++++++++++++++------------ 6 files changed, 91 insertions(+), 73 deletions(-) delete mode 100644 popUp.html create mode 100644 popup.html diff --git a/background.js b/background.js index 6b887f2..5a15820 100644 --- a/background.js +++ b/background.js @@ -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 }); diff --git a/button.css b/button.css index 9d6b375..e69de29 100644 --- a/button.css +++ b/button.css @@ -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; -} diff --git a/manifest.json b/manifest.json index d045aa4..ef6480d 100644 --- a/manifest.json +++ b/manifest.json @@ -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": [""], + "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" + } + } } diff --git a/popUp.html b/popUp.html deleted file mode 100644 index ff1d67a..0000000 --- a/popUp.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..c6aa684 --- /dev/null +++ b/popup.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/popup.js b/popup.js index 562f9e1..7389f03 100644 --- a/popup.js +++ b/popup.js @@ -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; }