2012-05-16 2 views
0

브라우저 시작시 백그라운드 페이지에서 로그인 코드를 한 번만 실행하려고합니다. 하지만 bgpage의 글로벌 코드는 매번 클릭 할 때마다 실행됩니다. 또한 다른 범위에서 실행됩니다. 이 문제를 해결할 수 있습니까?백그라운드 코드가 팝업으로 실행됩니다.

// background.js 
var someCustomType = new SomeCustomType(); 

function SomeCustomType() { 
    this.firstProperty; 
} 

function thisShouldBeCalledOnce() { 
    someCustomType.firstProperty = 'defined'; 
    alert('someCustomType.firstProperty=' + someCustomType.firstProperty); 
    console.log('thisShouldBeCalledOnce'); 
} 

if (true) { 
    // Alwase 'undefined' 
    alert('someCustomType.firstProperty=' + someCustomType.firstProperty); 
    thisShouldBeCalledOnce(); 
} 

답변

0

간단한 코드 예제 :

manifest.json을

{ 
    "name": "My First Extension", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "The first extension that I made.", 
    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    }, 
    "background": { 
    "scripts": ["background.js"] 
    }, 
    "permissions": [ 
    "tabs", 
    "http://*/*", 
    "https://*/*" 
    ] 
} 

popup.html : 그냥 더미 코드 :

<html> 
<head> 
</head> 
<body> 
<div> 
    <p> sometext</p> 
    <button type="button">Click Me</button> 
</div> 
</body> 
</html> 

혈중 알코올 농도 kground.js :이 경우

var someCustomType = new SomeCustomType(); 
function SomeCustomType() { 
    this.firstProperty; 
} 
function thisShouldBeCalledOnce() { 
    someCustomType.firstProperty = 'defined'; 
    alert('someCustomType.firstProperty=' + someCustomType.firstProperty); 
    console.log('thisShouldBeCalledOnce'); 
} 
thisShouldBeCalledOnce(); 

는 경고가 열려 팝업에는 경고가 표시되지 않습니다 때, 확장을로드 할 때, 한 번에 해고되었다. 문안 인사.

+0

참, 내 버그를 발견했습니다.''는'popup.html'에있었습니다. – Vasya

관련 문제