2016-08-17 5 views
0

알고 싶습니다. 배경 페이지에서 manifest.json의 content_scripts.matches과 일치하는 모든 페이지를 다시로드하는 방법이 있습니까? 아니면 내 코드에서 이것을 다른 곳에서 정의해야하고 탭을 반복하여 일치하는지 확인해야합니까?Chrome 확장 프로그램 - 일치하는 탭 다시로드

미리 감사드립니다.

+2

[chrome.runtime.getManifest()] (https://developer.chrome.com/extensions/runtime#method-getManifest) +'matches'를 regexps ('*'->'. *?') + 각 탭의 URL을 수동으로 비교합니다. – wOxxOm

+0

직접적인 방법이 없을 것 같습니까? 각 탭 (콘텐츠 스크립트)에서 신호를 보내고 백그라운드에서 tabId를 저장 한 다음이 탭을 새로 고치는 방법은 어떻습니까? –

+0

'getManifest' 할 것입니다! 답변으로 게시하려면 확인해 보겠습니다. –

답변

1

chrome.runtime.getManifest()을 사용하여 모든 콘텐츠 스크립트 선언을 가져오고 matches 및 기타 유형의 패턴을 regexps로 수동 변환하고 모든 브라우저 탭 URL을 확인하십시오.

이것은 제외 된 URL을 고려하지 않은 단순화 된 버전입니다.

var matches = []; 

chrome.runtime.getManifest().content_scripts.forEach(function(cs) { 
    Array.prototype.push.apply(matches, (cs.matches || []).map(matchToRegexp)); 
    Array.prototype.push.apply(matches, (cs.include_globs || []).map(globToRegexp)); 

    function matchToRegexp(match) { 
     return match.replace(/[{}()\[\]\\.+?^$|]/g, "\\$&") 
        .replace(/\*/g, '.*?'); 
    } 
    function globToRegexp(glob) { 
     return glob.replace(/[{}()\[\]\\.+^$|]/g, "\\$&") 
        .replace(/\?/g, '.') 
        .replace(/\*/g, '.*?'); 
    } 
}); 

var hasAllUrls = matches.indexOf('<all_urls>') >= 0 || matches.indexOf('*://*/*/') >= 0; 
var rxMatches = hasAllUrls ? /^(https?|file|ftp):\/\/.+/ 
          : new RegExp('^' + matches.join('$|^') + '$'); 

chrome.windows.getAll({ 
    populate: true, 
    windowTypes: ['normal', 'panel', 'popup'], 
}, function(windows) { 
    windows.forEach(function(window) { 
     window.tabs.forEach(function(tab) { 
      if (rxMatches.test(tab.url)) { 
       chrome.tabs.reload(tab.id); 
      } 
     }); 
    }); 
}); 

테스트되지 않음. 문제가 발생한 경우 디버그하고 수정하고이 대답을 편집하십시오.

더 정확한 glob-to-regexp 변환 기능을 보려면 this answer을 참조하십시오.