1

I reworked the "Clip to DEVONthink" to use in Opera 15+. The problem I'm left with is that the extension only works after a browser restart.

Update 1: Tested on Mac OS X 10.9.2 with Opera 21.0.1432.67, Opera Next 22.0.1471.40 and Google Chrome 35.0.1916.114. They all behave the same.

Update 2: Opera's own example for message passing got the same behavior. I'm left with the question if that's the expected behavior.

There's a background script defined in manifest.json:

"background": {
  "scripts": [ "main.js" ]
},

and a content script:

"content_scripts": [ {
  "js": [ "add_listener.js" ],
  "matches": [ "http://*/*", "https://*/*" ],
}],

... and in main.js in chrome.browserAction.onClicked.addListener a message is send to the content script to request page title & content, etc.

chrome.browserAction.onClicked.addListener(
    function (tab) {
        chrome.tabs.sendMessage(tab.id, {line: 'getdevonthinkurl' }); 
    }
 );

... and the content script sends a message back:

chrome.runtime.onMessage.addListener(
    function (request, sender){
        if (request.line=='getdevonthinkurl'){
        chrome.runtime.sendMessage({devonthinkurl:getDEVONthinkURL()});
        }
    }
);

... and that message is received by the main.js background script:

chrome.runtime.onMessage.addListener(
    function (request, sender) {
        if (request.devonthinkurl){
            chrome.tabs.update(sender.tab.id, {"url": request.devonthinkurl});
        }
    }
);

As mentioned above it works perfect after a browser restart but it don't understand why it won't work without.

Does anyone have got an idea (I may add I'm not strong at programming and there's maybe an fundamental flaw in the design)?

boretom
  • 121
  • 5

1 Answers1

1

The only thing which comes to my mind is that you reloading extension, but just after this step you are not refreshing page where content script is injected. Is there any error in background page? I tried several times and it works for me. Anyway you can use callback in message which can be more readable.

So main.js can look like:

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.sendMessage(tab.id, {line: 'getdevonthinkurl'},
        function(response){
            chrome.tabs.update(tab.id, {'url': request.devonthinkurl});
    }); 
});

And add_listener.js like this:

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse){
        if (request.line=='getdevonthinkurl'){
            sendResponse({devonthinkurl:getDEVONthinkURL()});
        }
    }
 );
  • Thanks for the code, it's a lot cleaner! Won't work without restart though. I get ` Stack trace: TypeError: Cannot read property 'devonthinkurl' of undefined at chrome-extension://bpcnacelhegpfbkjkbghdnbhoddaoapd/main.js:7:60 at disconnectListener (extensions::messaging:335:9)` (the stack exceed this comment length). – boretom May 30 '14 at 08:55
  • Gist of complete error [here](https://gist.github.com/boretom/dc92dea6d94e179e4024) – boretom May 30 '14 at 09:04
  • 1
    This error always occurs when there is no connection between background process and content script. Are you always reloading tab after reloading extension and before you click browser action button? – Sławomir Kaczorowski May 30 '14 at 09:34
  • You're a genius - reloading the page is what fixed it. So after installing the extension I have to reload ever website I want to use it in. Mmmhh, have to think of how to do that nicely. – boretom May 30 '14 at 10:08
  • Reloading the page did not fix the issue for me. The browser still has to be restarted and then it all works fine.. Strange. – SudoPlz Feb 12 '16 at 15:16