I am developing chrome extension. I want to connect some API to current tab after click on button in popup.html
. I use this code in popup.js
:
$('button').click(function() {
chrome.tabs.executeScript({
file: 'js/ymaps.js'
}, function() {});
});
In ymaps.js
I use following code to connect API to current tab:
var script = document.createElement('script');
script.src = "http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU";
document.getElementsByTagName('head')[0].appendChild(script);
This API is needed to use Yandex Maps. So, after that code I create <div>
where map should be placed:
$('body').append('<div id="ymapsbox"></div>');
And this simple code only loads map to created <div>
:
ymaps.ready(init);//Waits DOM loaded and run function
var myMap;
function init() {
myMap = new ymaps.Map("ymapsbox", {
center: [55.76, 37.64],
zoom: 7
});
}
I think, everything is clear, and if you are still reading, I'll explain what is the problem.
When I click on button in my popup.html
I get in Chrome's console Uncaught ReferenceError: ymaps is not defined
. Seems like api library isn't connected. BUT! When I manually type in console ymaps
- I get list of available methods, so library is connected. So why when I call ymaps
-object from executed .js
-file I get such an error?
UPD: I also tried to wrap ymaps.ready(init)
in $(document).ready()
function:
$(document).ready(function() {
ymaps.ready(init);
})
But error is still appearing. Man below said that api library maybe isn't loaded yet. But this code produces error too.
setTimeout(function() {
ymaps.ready(init);
}, 1500);
I even tried to do such a way...
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
if (changeInfo.status == "complete") {
chrome.tabs.executeScript({
file: 'js/gmm/yandexmaps.js'
});
}
});