http://stackoverflow.com/questions/10527625/google-chrome-extension-script-injections
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"name": "Chrome Extension",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [{
"matches": ["http://pagetoinject/script/into/*"],
"js": ["contentscript.js"]
}]
contenscript.js
var s = document.createElement('script');
s.src = chrome.extension.getURL("script.js");
(document.head||document.documentElement).appendChild(s);
s.parentNode.removeChild(s);
also tried this method with no success
var s = document.createElement('script');
s.src = chrome.extension.getURL("script.js");
s.onload = function() {
this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);
and I keep getting this error on my javascript console.
so the answer is :
{ "name": "Chrome Extension", "version": "1.0", "manifest_version": 2, "content_scripts": [{ "matches": ["http://pagetoinject/script/into/*"], "js": ["contentscript.js"] }], "web_accessible_resources": ["script.js"] }
but i'm not sure the other one says inject by string :
Content scripts are executed in an isolated environment. You have to inject thestate
method in the page itself.When you have to want to use one of thechrome.*
APIs in the script, you have to implement a special event handler, as described in this answer: Chrome extension - retrieving Gmail's original message.Otherwise, if you don't have to usechrome.*
APIs, I strongly recommend to inject all of your JS code in the page via a dynamictag:
inject is ok, of cause you need to active the eval option.Method 1: Inject another file
This is the easiest/best method when you have lots of code. Include your actual JS code in a file, sayscript.js
. Then let your content script be as follows (explained here: Google Chome “Application Shortcut” Custom Javascript):var s = document.createElement('script'); s.src = chrome.extension.getURL("script.js"); s.onload = function() { this.parentNode.removeChild(this); }; (document.head||document.documentElement).appendChild(s);
When your extension's manifest specifies"manifest_version": 2
, the injectedscript.js
file has to be added to the"web_accessible_resources"
section. For an explanation and example, see this answer.Method 2: Inject embedded code
This method is useful when you want to quickly run a small piece of code. (See also: How to disable facebook hotkeys with Chrome extension?).var actualCode = ['/* Code here. Example: */alert(0);', ' // Beware! This array have to be joined', ' // using a newline. Otherwise, missing semicolons', ' // or single-line comments (//) will mess up your', ' // code ----->'].join('\n'); var script = document.createElement('script'); script.textContent = actualCode; (document.head||document.documentElement).appendChild(script); script.parentNode.removeChild(script);
Method 2b: Using a function
For a big chunk of code, quoting the string is not feasible. Instead of using an array, a function can be used, and stringified:var actualCode = '(' + function() { // All code is executed in a local scope. // For example, the following does NOT overwrite the global `alert` method var alert = null; // To overwrite a global variable, prefix `window`: window.alert = null; } + ')();'; var script = document.createElement('script'); script.textContent = actualCode; (document.head||document.documentElement).appendChild(script); script.parentNode.removeChild(script);
This method works, because the+
operator on strings and a function converts all objects to a string. If you intend on using the code more than once, it's wise to create a function to avoid code repetition. An implementation might look like:function injectScript(func) { var actualCode = '(' + func + ')();' ... } injectScript(function() { alert("Injected script"); });
Note: Since the function is serialized, the original scope, and all bound properties are lost!var scriptToInject = function() { console.log(typeof scriptToInject); }; injectScript(scriptToInject); // Console output: "undefined"
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
评论
发表评论