Created
November 10, 2014 23:01
-
-
Save lmccart/5d29b3f1ac14ed2f1375 to your computer and use it in GitHub Desktop.
popup to main tab inject (chrome extension)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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