0

我正在嘗試類似於Selenium IDE(瀏覽器擴展)。 我想:Chrome擴展如何打開新窗口並從活動標籤發送信息

1打開新窗口的活動標籤

2次的行動記錄下來,並將其發送到我的新窗口

但我做錯了什麼,因爲我收到錯誤

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-extension://djnljameancbknhjdldffnejafgfkamf/dialog.html". Extension manifest must request permission to access this host. 
    at chrome-extension://djnljameancbknhjdldffnejafgfkamf/background.js:35:21 

manifest.json的

{ 
    "name": "Dialog tester", 
    "version": "1.0", 
    "manifest_version": 2, 
    "background": { 
    "scripts": [ 
     "background.js" 
    ], 
    "persistent": false 
    }, 
    "content_scripts": [ 
    { 
     "matches": [ 
     "<all_urls>" 
     ], 
     "js": [ 
     "open-dialog.js" 
     ] 
    } 
    ], 
    "permissions": [ 
    "tabs", 
    "<all_urls>" 
    ], 
    "web_accessible_resources": [ 
    "dialog.html", 
    "style.css" 
    ] 
} 

background.js

// Handle requests for passwords 

var createdWindow; 

chrome.runtime.onMessage.addListener(function(request) { 
    if (request.type === 'request_password') { 
     chrome.tabs.create({ 
      url: chrome.extension.getURL('dialog.html'), 
      active: false 
     }, function(tab) { 
      // After the tab has been created, open a window to inject the tab 
      chrome.windows.create({ 
       tabId: tab.id, 
       type: 'popup', 
       focused: true 
       // incognito, top, left, ... 
      }, function (window) { 
       console.log(window); 
       // Trace tab id which is created with this query 
       createdWindow = window.tabs[0].id 
      }); 
     }); 
     document.writeln("fsfs"); 
    } 
}); 
function setPassword(password) { 
    // Do something, eg..: 
    console.log(password); 
}; 

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { 
    // Inject script into chosen tab after it is loaded completely 
    if (tabId == createdWindow && changeInfo.status == "complete") { 
     // Inject Jquery and in current tab 
     chrome.tabs.executeScript(tabId, { 
      "file": "jquery.js" 
     }, function() { 
      // I am in call back 
      console.log("Injected some jquery "); 
     }); 
    } 
}); 

dialog.html

<!DOCTYPE html><html><head><title>Dialog test</title></head><body> 
<form> 
    <input id="pass" type="password"> 
    <input type="submit" value="OK"> 
</form> 
<script src="dialog.js"></script> 
</body></html> 

dialog.js

document.forms[0].onsubmit = function(e) { 
    e.preventDefault(); // Prevent submission 
    var password = document.getElementById('pass').value; 
    chrome.runtime.getBackgroundPage(function(bgWindow) { 
     bgWindow.setPassword(password); 
     window.close();  // Close dialog 
    }); 
}; 

開dialog.js

if (confirm('Open dialog for testing?')) 
    chrome.runtime.sendMessage({type:'request_password'}); 

任何想法是什麼錯?

回答

0

您不能將內容腳本插入到具有chrome-extension://方案的擴展頁面中。只需簡單地引用腳本內部頁(dialog.html):

<script src="jquery.js"></script> 

此外,創建具有popup類型的窗口有沒有必要做所有你做的。 Create它直接:

chrome.runtime.onMessage.addListener(function(request) { 
    if (request.type === 'request_password') { 
     chrome.windows.create({ 
      url: '/dialog.html', 
      type: 'popup', width: 400, height: 400, 
     }); 
    } 
}); 
+0

謝謝你的興趣。但是,如果我不能將腳本注入到chrome-extension://方案文件中,那麼我如何通過活動選項卡上的用戶操作更改彈出窗口的內容?你認爲這是可能的嗎? –

+0

您可以使用條件並顯示/隱藏HTML的部分內容。或者使用chrome.browserAction.setPopup。 – wOxxOm

+0

但是在這種方法中,我看到我無法登錄彈出所有用戶在活動選項卡上執行的操作?例如,我想登錄彈出用戶點擊鏈接名稱X,用戶單擊按鈕名稱Y等 –

相關問題