I hope it’s possible but I couldn’t make it work somehow. I want to attach a script located on disk (for ex: C:\path\to\content_script.js) to the web page using tab.attach() method. Is it possible to run script located other than addon’s directory?
thank you. but I am not developing web extension. I mean “https://developer.mozilla.org/en-US/Add-ons/SDK” by addon sdk. I can connect a sqlite file located on local disk, too. I thought a script file can be opened from local disk. There are Low-Level APIs, but I’m very new at addons and don’t know how to do it.
It reads the .js file but can’t convert to appropriate type for tab.attach() method. I tried with encoding=“b” and without encoding. It gives the same error like this.
message:“Unsupported contentScriptFile url: data:text/javascript;base64,LyogV2hpY2ggUHJvZHVjdCBJcyBUaGUgQmVzdCBNYXRjaCAqLwoKdmFyIHF1ZXN0aW9uTmFtZXMgPSBbCgkid2hpY2hfc3VnZ2VzdGlvbl9tYXRjaGVzIgpdOwoKdmFyIGFjY3VyYWN5VXBwZXJMaW1pdCA9IDEwMCwKCWFjY3VyYWN5TG93 VERY LONG RANDOM CHARACTERS HERE ==”
I’m sorry. For some reason I was under the impression that you need an URI for tab.attach(). It should work to use the contentScript option as a plain string, without the base64 encoding (which is not random :D):
const File = require("sdk/io/file");
// ...
const path = '...'; // not sure if forward / backward slashes matter
if (!File.exists(path)) { throw new Error(`Couldn't find the file "${ path }"`); }
const file = File.read(path, ''); // encoding, non-binary, i.e. text. Encoding should default to utf8, but you should test it if it matters
// now attach `file`:
tab.attach({ contentScript: file, });