2017-11-30 7 views
0

お互いに隣り合っている2つのタブを再読み込みしたい。それは動作しますが、一旦タブを離れると(例えば、別のタブをクリックするか)、ページ上の他の場所をクリックすると、ページが更新されなくなります。どうして?ページやブラウザをどこでクリックしても、それらのページをリフレッシュしておきたいです。Chrome拡張機能を介して2つの非アクティブなタブを同時に再読み込みするにはどうすればよいですか?

は、ここで私は、popup.htmlを削除背景部分を追加し、それが今の私の作品content.js

setInterval(function() { 

    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { 
    var currentIndex = tabs[0].index; 
    var leftIndex = tabs[0].index - 1; 
    chrome.tabs.query({currentWindow: true}, function (tabs) {// This will return all tabs in current window 
     chrome.tabs.reload(tabs[currentIndex].id); 
     chrome.tabs.reload(tabs[leftIndex].id); 
    }); 
    }) 

}, 2000); 

manifest.jsonを

{ 
    "manifest_version": 2, 
    "name": "extension", 
    "version": "1.0", 
    "description": "My first Chrome extension.", 
    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    }, 
    "content_scripts": [ 
    { 
     "matches": [ 
     "<all_urls>" 
     ], 
     "js": [ 
     "jquery.min.js", 
     "content.js" 
     ] 
    } 
    ] 
} 

popup.html

<!doctype html> 
<html> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
    <title>My cool extension</title> 
    <script src="content.js"></script> 
</head> 
<body> 
</body> 
</html> 
+0

chrome.tabs.queryがで失敗どこに(両方のWebページ内でcontent.jsを実行していますそれが許可されていないのでエラーが発生します)、タブを切り替えると非表示になるbrowserActionポップアップの内側に表示されます。正しい解決方法は、コードをイベントページスクリプトに移動することです。ドキュメントを参照してください。おそらくメッセージングが必要になります(chrome.runtime APIを参照)。 – wOxxOm

+0

@wOxxOm私は別の方法で問題を解決しましたが、ありがとう、私は今それを探しています。 – salep

答えて

0

です。ここでは、最終的なスクリプトです:

content.jsは

setInterval(function() { 

    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { 
    var currentIndex = tabs[0].index; 
    var leftIndex = tabs[0].index - 1; 
    chrome.tabs.query({currentWindow: true}, function (tabs) {// This will return all tabs in current window 
     chrome.tabs.reload(tabs[0].id); 
     chrome.tabs.reload(tabs[1].id); 
    }); 
    }); 

}, 2000); 

manifest.jsonを

{ 
    "manifest_version": 2, 
    "name": "extension", 
    "version": "1.0", 
    "description": "My first Chrome extension.", 
    "browser_action": { 
    "default_icon": "icon.png" 
    }, 
    "background": { "scripts": [ "content.js" ], "persistent": true }, 
    "content_scripts": [ 
    { 
     "matches": [ 
     "<all_urls>" 
     ], 
     "js": [ 
     "jquery.min.js", 
     "content.js" 
     ] 
    } 
    ] 
} 
関連する問題