0

I'm trying to build a Chrome Extension to add a personalized signature/message to the end of a message/comment in Trix

From the reading of the docs and other similar issues I've found it seems pretty straightforward. Something like this from my content.js file...

'use strict'
var sig = '<p>Testing 1,2</p>';
//chrome.storage.sync.get(null, function (data) {
//    sig = data.signature;
//});
document.addEventListener("trix-initialize", function (event) {
    event.target.editor.loadHTML(sig);
});

...should do the trick. But I keep getting this error...

Uncaught TypeError: Cannot read property 'loadHTML' of undefined at HTMLDocument.<anonymous>

I've included my manifest, script, and content files here after getting a bit more guidance

manifest.json

{
    "manifest_version": 2,
    "name": "BaseCamp Signature", 
    "description": "This extension adds signature to Basecamp", 
    "version": "1.0",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html",
        "default_title": "Title!"
    },
    "permissions": [
        "storage", 
        "activeTab",
        "tabs",
        "https://3.basecamp.com/*"    
    ],
    "content_scripts": [
        {
            "matches": ["https://3.basecamp.com/*/*"], 
            "all_frames": true,
            "js": ["content.js"],
            "run_at": "document_end"
        }
    ],
    "web_accessible_resources": [
        "script.js",  
    ]
}

script.js

'use strict'
var sig = '<p>Testing 1,2</p>';
//chrome.storage.sync.get(null, function (data) {
//    sig = data.signature;
//});
document.addEventListener("trix-initialize", function (event) {
    event.target.editor.loadHTML(sig);
});

content.js

var s = document.createElement('script');
// TODO: add "script.js" to web_accessible_resources in manifest.json
s.src = chrome.extension.getURL('script.js');
s.onload = function() {
    this.remove();
};
(document.head || document.documentElement).appendChild(s);
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • 2
    Content scripts are isolated from page code so you need to [Insert code into the page context using a content script](//stackoverflow.com/q/9515704) - insert the part of your code that reads the *expando property* `editor` and calls its loadHTML. – wOxxOm Jul 16 '17 at 17:01
  • I was under the impression my content.js file was what runs between my background.js and actual DOM which allows for DOM manipulation. Is this incorrect thinking. I've added my manifest as an update to the initial question –  Jul 16 '17 at 17:08
  • 2
    Like I said, the `editor` **expando property** is a js object/variable set by the page script so the content scripts, which run in an *isolated world*, cannot access it. The linked topic shows a workaround. – wOxxOm Jul 16 '17 at 17:17

0 Answers0