I am extending LipSurf with some personal plugins and I have a problem to launch a bookmarlet.
A bookmarlet is a javascript code fragment that it is saved in Chrome like a bookmark.
My code is trying to execute a macro previusly recorded with the IMacros addon for Chrome.
You should use pageFn to run the code that the bookmarklet would normally. But there is no reason to go through the bookmarklet that I can think of - you can execute the js directly.
You’re using fn – this executes in the background/extension context and doesn’t have access to the document. You want to use pageFn so it executes on in the page context. More details here:
Did you try the Hello World plugin in the tutorial? I’ve just verified and it’s working perfectly for me - creating an alert on the page.
What exactly is not working?
Is the plugin building properly? Are you writing it in TypeScript? Any build errors? Does it load and show in the options and help?
(if you aren’t writing it in TS, then I would use the HelloWorld plugin as a starting point for the plain js version);
import PluginBase from 'chrome-extension://lnnmjmalakahagblkkcnjkoaihlfglon/dist/modules/plugin-base.js';import ExtensionUtil from 'chrome-extension://lnnmjmalakahagblkkcnjkoaihlfglon/dist/modules/extension-util.js';/// <reference types="lipsurf-plugin-types"/>
var HelloWorld = { ...PluginBase, ...{
niceName: 'Hello World',
languages: {},
description: 'A "hello world" plugin that works on the lipsurf.com domain.',
// a RegEx that must match against the current tab's url for the plugin to be active (all of it's commands minus global commands)
match: /.*\.lipsurf.com/,
version: '1.0.0',
commands: [{
name: 'Respond',
description: 'Respond with something incredibly insightful to the user.',
// what the user actually has to say to run this command
match: 'hello world',
// the js that's run on the page
pageFn: function () {
alert('Hello, Developer!');
}
}]
} };
export default HelloWorld;
LS-SPLITallPlugins.HelloWorld = (() => { /// <reference types="lipsurf-plugin-types"/>
var HelloWorld = { ...PluginBase, ...{
commands: {
"Respond": {
// the js that's run on the page
pageFn: function () {
alert('Hello, Developer!');
}
}
}
} };
return HelloWorld;
})()
Check the console when you load the plugin (via “load a local plugin” button, any error message there)?
You might have to up the version number of the plugin to install a newer/modified version.
Are you sure the page you’re running it on matches the regex in the plugins match property?
I think I didn’t explain the issue correctly: sorry!!
My plugin works well using “fn” (no with “pageFn”), and it’s able to asign the values to the variables, to calls funcions, to show the “alerts”,… but it fails when I try to launch the bookmarklet:
var evt = document.createEvent(“CustomEvent”);
evt.initCustomEvent(“iMacrosRunMacro”, true, true, macro);
window.dispatchEvent(evt);
And I am sure this code works because it does in a simple .html page:
div {padding:50px;
background-color: Tomato;
color: white;}
The createEvent() Method
The createEvent() method allows you to simulate any event.
In this example, the red div will get a new star every time you mouse over it:
I think I understand now. Did you look at the console? You can also put a debugger statement in the pageFn and step through it with the console open when you execute the command.
Also, does it work when you put the code in the chrome console (f12) directly?