Skip to content

Instantly share code, notes, and snippets.

@lmccart
Created November 10, 2014 23:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmccart/5d29b3f1ac14ed2f1375 to your computer and use it in GitHub Desktop.
Save lmccart/5d29b3f1ac14ed2f1375 to your computer and use it in GitHub Desktop.
popup to main tab inject (chrome extension)
$("#change").click(function() {
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?id=5128581&units=imperial",
dataType: 'json',
success: function(data) {
changeMainPage(data);
},
error: function() {
alert("error");
}
});
});
function changeMainPage(data) {
// inject jquery
chrome.tabs.executeScript(null,
{file:"jquery.js"});
// inject custom js
chrome.tabs.executeScript(null, {
// convert json data into variable string for passing
code: 'var myData = ' + JSON.stringify(data)
}, function() {
// inject actual script
chrome.tabs.executeScript(null, {file: 'inject.js'});
});
}
// this gets injected on the main tab page
// we could just log the data to console
console.log(myData);
// or parse the data back from string into object
var data = JSON.parse(myData);
// access data parts, for example
var x = data.weather.clouds;
// modify dom based on data
console.log('test')
$('body').css('background', 'pink');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment