Injecting Functions from Content Scripts - Show "sourceless code"

RESOLVED WORKSFORME

Status

()

Toolkit
WebExtensions: Developer Tools
P5
normal
RESOLVED WORKSFORME
2 years ago
6 months ago

People

(Reporter: ido.y, Unassigned)

Tracking

42 Branch
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: triaged)

Attachments

(1 attachment)

687 bytes, application/zip
Details
(Reporter)

Description

2 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

2 years ago
Component: Untriaged → Web Apps
OS: Unspecified → All
Hardware: Unspecified → All
(Reporter)

Updated

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

Updated

2 years ago
Blocks: 1214433
Priority: -- → P4
Can you please attach a testcase add-on?
(Reporter)

Comment 2

2 years ago
Created attachment 8675356 [details]
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

2 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

2 years ago
Flags: blocking-webextensions+

Updated

a year ago
Whiteboard: triaged

Updated

a year ago
Flags: blocking-webextensions+ → blocking-webextensions-

Updated

8 months ago
Component: WebExtensions: Untriaged → WebExtensions: Developer tools
Flags: blocking-webextensions-
Priority: P4 → P5
Status: UNCONFIRMED → RESOLVED
Last Resolved: 6 months ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.