Added popup button to refresh ip

This commit is contained in:
z0ccc 2022-04-14 00:50:04 -04:00
parent 4f26e63d21
commit 5d33decc7d
3 changed files with 49 additions and 42 deletions

View file

@ -3,7 +3,7 @@
"name": "Vytal", "name": "Vytal",
"description": "Vytal", "description": "Vytal",
"options_page": "options.html", "options_page": "options.html",
"permissions": ["debugger", "activeTab"], "permissions": ["storage", "debugger", "activeTab"],
"background": { "service_worker": "background.bundle.js" }, "background": { "service_worker": "background.bundle.js" },
"action": { "action": {
"default_popup": "popup.html", "default_popup": "popup.html",

View file

@ -1,8 +1,22 @@
import countryLocales from './countryLocales'; import countryLocales from './countryLocales';
const attachTab = (tabId, ipData) => { const attachTab = (tabId, ipData) => {
console.log(1);
chrome.debugger.attach({ tabId: tabId }, '1.3', function () { chrome.debugger.attach({ tabId: tabId }, '1.3', function () {
console.log(2, chrome.runtime.lastError);
if (!chrome.runtime.lastError) { if (!chrome.runtime.lastError) {
console.log(3);
chrome.debugger.sendCommand(
{ tabId: tabId },
'Emulation.clearGeolocationOverride'
);
chrome.debugger.sendCommand(
{ tabId: tabId },
'Emulation.clearIdleOverride'
);
chrome.debugger.sendCommand( chrome.debugger.sendCommand(
{ tabId: tabId }, { tabId: tabId },
@ -20,9 +34,9 @@ const attachTab = (tabId, ipData) => {
{ tabId: tabId }, { tabId: tabId },
'Emulation.setGeolocationOverride', 'Emulation.setGeolocationOverride',
{ {
latitude: ipData.lat, latitude: ipData.lat,
longitude: ipData.lon, longitude: ipData.lon,
accuracy: 1, accuracy: 1,
} }
); );
@ -41,22 +55,24 @@ const attachTab = (tabId, ipData) => {
}; };
chrome.tabs.onUpdated.addListener((tabId, change, tab) => { chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
// chrome.debugger.getTargets((tabs) => {
// let tab = tabs.find((obj) => {
// return obj.tabId === tabId;
// });
chrome.debugger.getTargets((results) => { // if (!tab.attached) {
chrome.storage.sync.get(['ipData'], (result) => {
let result = results.find((obj) => { console.log(result.ipData)
return obj.tabId === tabId; attachTab(tabId, result.ipData);
}); });
// }
if (!result.attached) { // });
fetch('http://ip-api.com/json/')
.then(response => response.json())
.then(ipData => attachTab(tabId, ipData));
}
});
}); });
// fetch('http://ip-api.com/json/')
// .then((response) => response.json())
// .then((ipData) => {});
// Detects if there are posts for current url // Detects if there are posts for current url
// chrome.tabs.onCreated.addListener((tab) => { // chrome.tabs.onCreated.addListener((tab) => {
// console.log(tab.id) // console.log(tab.id)
@ -82,12 +98,11 @@ chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
// }); // });
// }); // });
// chrome.debugger.sendCommand(
// chrome.debugger.sendCommand( // { tabId: tabId },
// { tabId: tabId }, // "Emulation.setAutomationOverride",
// "Emulation.setAutomationOverride", // {
// { // enabled:
// enabled: // true,
// true, // },
// }, // );
// );

View file

@ -1,25 +1,17 @@
import React from 'react'; import React from 'react';
import logo from '../../assets/img/logo.svg';
// import Greetings from '../../containers/Greetings/Greetings';
import './Popup.css'; import './Popup.css';
const getIP = () => fetch('http://ip-api.com/json/')
.then((response) => response.json())
.then((ipData) => {
chrome.storage.sync.set({ ipData });
});
const Popup = () => { const Popup = () => {
return ( return (
<div className="App"> <div className="App">
<header className="App-header"> <button type="button" onClick={() => getIP()}>Click Me!</button>
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/pages/Popup/Popup.jsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React!
</a>
</header>
</div> </div>
); );
}; };