The My Opera forums have been replaced with forums.opera.com. Please head over there to discuss Opera's products and features
See the new ForumsYou need to be logged in to post in the forums. If you do not have an account, please sign up first.
There is no way to save to a local file. You can of course load and save preferences with the widget.preferences object. widget.preferences is available in the background script, popups, extension pages and injected scripts.
Of course, from my experience with using widget.preferences in injected scripts, it doesn't seem to work in private tabs. If you need your extension to work in private tabs, you will have to use opera.extension.postMessage from the injected script to send data back to the background script, then save the data to widget.preferences from the background script.
Of course, from my experience with using widget.preferences in injected scripts, it doesn't seem to work in private tabs. If you need your extension to work in private tabs, you will have to use opera.extension.postMessage from the injected script to send data back to the background script, then save the data to widget.preferences from the background script.
Opera 12.15 - Win 8 Pro x64 All my Opera tools -Tab Vault: Save tabs for later -AutoStack: tabs opened from a stack stay there
I tried to "use opera.extension.postMessage from the injected script to send data back to the background script",
but background script cant read my message...
I did it so:
//injected
window.opera.postError('Sending message to background.js');
opera.extension.postMessage('LOAD');
//////////////////background
window.addEventListener('DOMContentLoaded', function() {
// Listen for messages from the UserJS.
opera.extension.onmessage = function(event){
window.opera.postError('Received message from base.js');
if (event.data === 'LOAD') {
button.badge.textContent = parseInt(button.badge.textContent) + 1;
window.alert("test");
}
}
}, false);
one more:
//injected
opera.extension.bgProcess.SavePrefs(mailz);
//bckgnd
function SavePrefs()
{
something to do
}
and more:
//bckgnd
window.addEventListener("load", setupConnection, false);
function setupConnection(){
opera.extension.onconnect = function(event){
opera.postError("sent message to injected script");
}
opera.extension.onmessage = function(event){
opera.postError("This is what I got from injected script: "+event.data);
}
//injected
opera.extension.onmessage = function(event){
// Get content of incoming message.
alert("test");
var message = event.data;
window.opera.postError("background process sent: " + message);
// Replies back to background process.
var reply = "background process's message only had " + (message ? message.length : 0) + " characters.";
event.source.postMessage(reply);
};
but background script cant read my message...
I did it so:
//injected
window.opera.postError('Sending message to background.js');
opera.extension.postMessage('LOAD');
//////////////////background
window.addEventListener('DOMContentLoaded', function() {
// Listen for messages from the UserJS.
opera.extension.onmessage = function(event){
window.opera.postError('Received message from base.js');
if (event.data === 'LOAD') {
button.badge.textContent = parseInt(button.badge.textContent) + 1;
window.alert("test");
}
}
}, false);
one more:
//injected
opera.extension.bgProcess.SavePrefs(mailz);
//bckgnd
function SavePrefs()
{
something to do
}
and more:
//bckgnd
window.addEventListener("load", setupConnection, false);
function setupConnection(){
opera.extension.onconnect = function(event){
opera.postError("sent message to injected script");
}
opera.extension.onmessage = function(event){
opera.postError("This is what I got from injected script: "+event.data);
}
//injected
opera.extension.onmessage = function(event){
// Get content of incoming message.
alert("test");
var message = event.data;
window.opera.postError("background process sent: " + message);
// Replies back to background process.
var reply = "background process's message only had " + (message ? message.length : 0) + " characters.";
event.source.postMessage(reply);
};
I don't see why the first example there wouldn't work, except that window.alert() doesn't do anything in the background process.
As for the second, injected scripts cannot use opera.extension.bgProcess (see the article on bgProcess). What you want to do is something like this:
You can send and receive entire objects as well as strings, which makes messaging much easier.
Also, on the third example, you are never sending a message from the background script to the injected script, so it will never respond. This should send a message when the script connects:
There is a bug in current versions of Opera though where onconnect may not fire for every page when you start Opera and Opera restores a session with a large number of tabs.
As for the second, injected scripts cannot use opera.extension.bgProcess (see the article on bgProcess). What you want to do is something like this:
//background script
addEventListener('load', function() {
// Listen for messages
opera.extension.onmessage = function(e) {
switch (e.data.action) {
case 'save prefs':
savePrefs(e.data.prefs);
break;
default: break;
}
}
// Saves all the values in the object "prefs" to widget.preferences
function savePrefs(prefs) {
for (var name in prefs) {
if (prefs.hasOwnProperty(name)) {
widget.preferences[name] = prefs[name];
console.log('saved: ' + name + ' = ' + prefs[name]);
}
}
}
}, false);
//injected script
// Send a request to save preferences
opera.extension.postMessage({
action: 'save prefs',
prefs: {
some_preference: 'some preference value',
color: '#FF0000',
rainbows: 2,
narwhals: 1,
},
});
You can send and receive entire objects as well as strings, which makes messaging much easier.
Also, on the third example, you are never sending a message from the background script to the injected script, so it will never respond. This should send a message when the script connects:
opera.extension.onconnect = function(e){
opera.postError("sent message to injected script");
e.source.postMessage('some message');
}
There is a bug in current versions of Opera though where onconnect may not fire for every page when you start Opera and Opera restores a session with a large number of tabs.
Opera 12.15 - Win 8 Pro x64 All my Opera tools -Tab Vault: Save tabs for later -AutoStack: tabs opened from a stack stay there
Forums » Dev.Opera » Opera Extensions Development Discussions