Skip to content

Instantly share code, notes, and snippets.

@dineshsprabu
Last active May 1, 2024 21:11
Show Gist options
  • Save dineshsprabu/911305e315c6cdcd4e23b243cd597c47 to your computer and use it in GitHub Desktop.
Save dineshsprabu/911305e315c6cdcd4e23b243cd597c47 to your computer and use it in GitHub Desktop.
[Chrome Extension] Chrome Extension Helper

Get current tab URL

Add below to permissions on manifest

"permissions": [
    "tabs",
    "http://*/*", 
    "https://*/*"
]
chrome.tabs.getCurrent(function(tab){
        console.log(tab.url);
    }
);

Alternative for above method.

chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
    // Do something
});

Get user location information

Add below to permissions on manifest.

"permissions": [
    "geolocation"
]

Below promisified method can be used for getting user lat and long information.

function getUserLocationInformation(){
	return new Promise(function(resolve, reject){
		if(navigator && navigator.geolocation){
			navigator.geolocation.getCurrentPosition(
				function(geoInfo){
					if(geoInfo){
						resolve({
							lat: geoInfo.coords.latitude,
							long: geoInfo.coords.longitude
						})
					}else{
						reject('Unable to fetch geo location');
					}
				})
		}
	}
}

Make an ajax call to the below URL to fetch Location information using Lat and Long from above.

latlong_string = '25.033747,121.534620'
http://maps.googleapis.com/maps/api/geocode/json?latlng='+latlong_string+'&sensor=true

Injecting JS into a page from extension

Reference Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment