:: ADVANCE ::

[Error] 전역변수 본문

Study/Chrome Extension

[Error] 전역변수

KSJ14 2015. 3. 9. 22:21
반응형
1
2
3
4
5
6
7
8
9
10
11
url = 0;
 
chrome.tabs.getSelected(nullfunction(tab)    {
// 지정된 창에서 선택한 탭을 가져온다.
    url = tab.url;
    document.getElementById('currentLink').innerHTML = url;
    // document.getElementById("id").innerHTML = "<h1>새로운 태그 삽입</hr>";
    // document.getElementById("id").src = "이미지경로";
});
 
console.log("url : " + url);

cs


url을 tab의 속성을 이용해서 가져온다.


chrome.tabs.getSelected를 이용해서 가져왔는데


전역변수 url을 선언하고 저장해서


함수 밖에서 불러오려고 하였는데 결과는 그대로 0 이었다.


stack overflow site를 참고하면


[참고] http://stackoverflow.com/questions/1979583/how-can-i-get-the-url-for-a-google-chrome-tab




The problem is that chrome.tabs.getSelected is asynchronous. This code below will generally not work as expected. The value of 'tablink' will still be undefined when it is written to the console because getSelected has not yet invoked the callback that resets the value:


chrome.tabs.getSelected가 비동기라서 문제이다.

값을 다시 설정 callback을 호출하지 않아서 console에 기록될 때 값은 여전히 정의되지 않는다.



라고 하는데 무슨소린지...



아무튼 결론은


The solution is to wrap the code where you will be using the value in a function and have that invoked by getSelected. In this way you are guaranteed to always have a value set, because your code will have to wait for the value to be provided before it is executed.


함수의 값을 사용하게 될 것을 통해 getSelected에 의해 호출한 코드를 래핑하는 것이다.

코드가 실행되기 전에 제공되어야 하는 값까지 기다릴 필요가 있기 때문에 이러한 방법으로,

항상 설정된 값을 갖도록 보장된다....



너무 구글 번역기만 돌린건지 이것 또한 무슨소린지 모르겠다



1
2
3
4
5
6
7
8
9
10
11
chrome.tabs.getSelected(nullfunction(tab)    {
// 지정된 창에서 선택한 탭을 가져온다.
    getURL(tab.url);
});
 
function getURL(taburl)    {
    console.log("url :" + taburl);
    document.getElementById('currentLink').innerHTML = taburl;
    // document.getElementById("id").innerHTML = "<h1>새로운 태그 삽입</hr>";
    // document.getElementById("id").src = "이미지경로";
}

cs



그렇지만 아직도 함수내에서는 작동하지만 전역변수로는 저장을 할 수가 없다....


아나.........


반응형

'Study > Chrome Extension' 카테고리의 다른 글

[Chrome Extension]  (0) 2015.05.19
Manifest.json 포맷 구조  (0) 2015.03.02
Comments