Chrome: message content - script in runtime.onInstalled

I am the creator of an addon called BeautifyTumblr that changes the look of Tumblr. I want the Chrome extension to automatically detect when it is updated and display the change log to the user. I use the event page with the chrome.runtime.onInstalled.addListener hook to determine when the update occurred, get the change log from the text file in the extension .. it all works fine, and then when I want to redirect it to my script content via chrome .tabs.sendmessage it just doesn't work, nothing happens, no errors, nothing. I'm at a dead end.

Any help would be greatly appreciated!

Event Page:

chrome.runtime.onInstalled.addListener(function (details) { "use strict"; if (details.reason === "install") { } else if (details.reason === "update") { var thisVersion = chrome.runtime.getManifest().version, xmlDom, xmlhttp; xmlDom = null; xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", chrome.extension.getURL("changelog.txt"), false); xmlhttp.send(null); xmlDom = xmlhttp.responseText; chrome.tabs.query({'url' : 'http://www.tumblr.com/*'}, function (tabs) { if (tabs.length > 0) { var mTab = tabs[0].id; chrome.tabs.update(mTab, {active: true}); setTimeout(chrome.tabs.sendMessage(mTab, {beautifyTumblrUpdate: xmlDom}), 500); } else { chrome.tabs.create({'url' : 'http://www.tumblr.com/dashboard'}, function (tab) { setTimeout(chrome.tabs.sendMessage(tab.id, {beautifyTumblrUpdate: xmlDom}), 500); }); } }); } }); 

Relevant code in Script content:

 chrome.runtime.onMessage.addListener( function (request, sender, sendResponse) { "use strict"; window.alert('test'); if (request.beautifyTumblrUpdate) { window.alert(request.beautifyTumblrUpdate); } else if (request.beautifyTumblrInstall) { window.alert(request.beautifyTumblrInstall); } } ); 
0
source share
2 answers

I see the same thing too. I am not 100% sure, but I think this is happening because chrome disconnects the connection between the background and the old content scripts at the time the extension is updated. Here is more information in this error: https://code.google.com/p/chromium/issues/detail?id=168263

0
source

just use the following code in the background,

 chrome.runtime.onInstalled.addListener(function(details){ if(details.reason == "install"){ chrome.tabs.create({ url: chrome.extension.getURL('welcome.html')}); } }); 
0
source

Source: https://habr.com/ru/post/952678/


All Articles