English 中文(简体)
在果园幼苗推广中使用外围 j
原标题:Loading external javascript in google chrome extension

我写了谷歌延伸版,它操纵了本页(实际上增加了一个纽子)。

In my content script, I want to load the Facebook Graph API:

$fbDiv = $(document.createElement( div )).attr( id ,  fb-root );
$fbScript = $(document.createElement( script )).attr( src ,  https://connect.facebook.net/en_US/all.js );
$(body).append($fbDiv);
$(body).append($fbScript);

console.log("fbScript: " + typeof $fbScript.get(0));
console.log("fbScript parent: " + typeof $fbScript.parent().get(0));
console.log("find through body: " + typeof $(body).find($fbScript.get(0)).get(0));

However, the script doesn t seem to added to body. Here s the console log:

fbScript: object
fbScript parent: undefined
find through body: undefined

任何关于我做什么错误的想法?

最佳回答

问题是,内容文字中的 Java本在自己的沙箱环境中运行,只能使用其他瓦 Java文字,其载荷有两种:

Via the manifest:

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "js": ["https://connect.facebook.net/en_US/all.js"]
    }
  ],
  ...
}

Or using Programmatic injection:

/* in background.html */
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null,
                       {file:"https://connect.facebook.net/en_US/all.js"});
});

1. 确保更新你的明确许可:

/* in manifest.json */
"permissions": [
    "tabs", "https://connect.facebook.net"
 ], 

Appending a script tag will in effect evaluate the JavaScript in the context of the containing page, outside of the JavaScript sandbox that your JavaScript has access to.

此外,由于《联邦公报》的文字要求“fb-root”在OMS内,你很可能需要采用方案办法,以便你能够首先用这个要素更新OMM,然后是

问题回答

如果你想把它装成内容文字:

fetch( https://example.com/content.js )
  .then(resp => resp.text())
  .then(eval)
  .catch(console.error)

If you want to load it as background script. Take https://example.com/bg.js for example.

  1. add the remote js script to the background page file, which is named background.html here
<!DOCTYPE html>
<html>
<head>
  <script src="https://example.com/bg.js"></script>
</head>
<body>
<div>Empty content</div>
</body>
</html>
  1. add https://example.com to the content_security_policy of the manifest.json:
"background": {
  "page": "background.html"
},
"content_security_policy": "script-src  self  https://example.com ; object-src  self ",

Requirements:

  1. The remote js script must be served via https instead of http.
  2. You should specify a page rather than a scripts array in the background section




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签