2015-06-24 4 views
6

Quindi sto provando a eseguire uno script da una fonte esterna come www.script.google.com in background.js. ma ottengo questo errore -chrome.tabs.executeScript genera l'errore "Unchecked runtime.lastError durante l'esecuzione di tabs.executeScript: impossibile accedere al contenuto dell'URL ..."

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/devtools.html?&remoteBase=https://chrome…&dockSide=undocked&toolbarColor=rgba(223,223,223,1)&textColor=rgba(0,0,0,1)". Extension manifest must request permission to access this host. 

quello che sto facendo sta inviando un messaggio da popup.js a background.js In popup.js -

chrome.runtime.sendMessage({type:"addtask"}); 

In background.js -

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){ 
    if(request.type == "addtask") 
    { 
     chrome.tabs.executeScript(null, 
         {file:"https://script.google.com/url of script....."}); 
    } 
}); 

mio manifest.json-

{ 
    "name": "Extension", 
    "version": "0.0.1", 
    "manifest_version": 2, 
    "browser_action": { 
     "default_popup": "popup.html" 
    }, 
    "background": { 
     "scripts": ["background.js"], 
     "persistent": false 
    }, 
    "content_scripts": [{ 
     "js": ["jquery.min.js","contentscript.js"], 
     "matches": ["http://*/*","https://*/*"], 
     "css" : ["feedback.css"] 
    }], 
    "permissions": [ 
      "storage","tabs","https://script.google.com" 
     ], 
    "web_accessible_resources": ["feedback.js","html2canvas.js","event.js"], 
    "content_security_policy": "script-src 'self' https://script.google.com/*; object-src 'self'" 
} 
+1

includere il codice rilevante che fa scattare questo errore. – Xan

+0

@Xan Ho aggiunto il codice. – Sid

+0

L'intenzione non è chiara al 100%. Stai provando a eseguire lo script esterno come script di contenuto o come script in background? – Xan

risposta

6

Pianura e diritta. Aggiungi *://*/* alle autorizzazioni.

+1

È incredibile. Dopo ore di ricerche, ho scoperto che i miei URL di autorizzazione erano errati: mancava il simbolo dell'asterisco finale, quindi corrispondeva solo alle home page. L'errore dell'ultima volta di Chrome è stato in qualche modo fuorviante, poiché censurava gli URL che cercavo di accedere con "data: text/html, chromewebdata" – Anonymous

0

Mentre il suggerimento ArtPip funziona in questo caso, spesso si desidera eseguire uno script su una scheda o su tutte le schede e gestire correttamente l'errore se le proprie autorizzazioni non consentono l'inserimento in quella scheda o in alcune delle schede.

Ecco un esempio di esecuzione di uno script su tutte le schede e correttamente gli errori di manipolazione:

tabs.forEach(tab => { 
    chrome.tabs.executeScript(tab.id, { file: filename }, result => { 
     const lastErr = chrome.runtime.lastError; 
     if (lastErr) console.log('tab: ' + tab.id + ' lastError: ' + JSON.stringify(lastErr)); 
    }); 
});