Injecting Functions from Content Scripts - Show "sourceless code"

RESOLVED WORKSFORME

Status

defect
P5
normal
RESOLVED WORKSFORME
4 years ago
11 months ago

People

(Reporter: ido.y, Unassigned)

Tracking

42 Branch
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: triaged)

Attachments

(1 attachment)

687 bytes, application/zip
Details
Reporter

Description

4 years ago
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36

Steps to reproduce:

we used a content script to inject script to the page

the function for injecting the script is
	function appendContentScript(id, content) {
		var scriptTag = document.createElement('script');
		scriptTag.id = id;
		scriptTag.type = 'text/javascript';
		scriptTag.appendChild( document.createTextNode(content) );
		head.appendChild(scriptTag);
	}


Actual results:

when we try to parse a function as content for example
function test() {
alert('hi, im test!');
}
we get the following script
<script type="text/javascript" id="SCRIPT_ID">function test() { [sourceless code] }</script>


Expected results:

<script type="text/javascript" id="SCRIPT_ID">function test() { alert('hi, im test!'); }</script>
Reporter

Updated

4 years ago
Component: Untriaged → Web Apps
OS: Unspecified → All
Hardware: Unspecified → All
Reporter

Updated

4 years ago
Component: Web Apps → WebExtensions
Product: Firefox → Toolkit

Updated

4 years ago
Blocks: webext
Priority: -- → P4
Can you please attach a testcase add-on?
Reporter

Comment 2

4 years ago
Posted file Extension.xpi
attached xpi
That add-on seems to work as expected for me.

In any case, I'd suggest converting the function to a string before passing it to createTextNode. And if you're using functions to inject code into pages like this, it would probably be best to always do something like `"(" + func + ")()"` rather than just injecting the bare function as a top-level function statement.
Reporter

Comment 4

4 years ago
when "running" the code in the console it seems to work fine (without converting the function)
should it be different ?

about the self execution - it was just an example :)

Updated

4 years ago
Flags: blocking-webextensions+

Updated

3 years ago
Whiteboard: triaged

Updated

3 years ago
Flags: blocking-webextensions+ → blocking-webextensions-

Updated

3 years ago
Component: WebExtensions: Untriaged → WebExtensions: Developer tools
Flags: blocking-webextensions-
Priority: P4 → P5
Status: UNCONFIRMED → RESOLVED
Last Resolved: 3 years ago
Resolution: --- → WORKSFORME

Updated

11 months ago
Product: Toolkit → WebExtensions
You need to log in before you can comment on or make changes to this bug.