Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to WebExtension (WIP) #176

Closed
wants to merge 33 commits into from
Closed

Port to WebExtension (WIP) #176

wants to merge 33 commits into from

Conversation

Cimbali
Copy link

@Cimbali Cimbali commented Apr 26, 2018

This is a port of CleanLinks to web extensions.

A few ways of cleaning links have already been implemented:

  • intercepting web requests,
  • cleaning all links in page,
  • intercepting clicks to clean pages
  • preferences loading/saving/updating
  • list of recently cleaned links for whitelisting
  • copying cleaned links to clipboard

I'm starting this PR so that anyone who wants to help, see the progress, or otherwise comment on this work can do so. In particular I think I didn't use most of the features of the add-on, so I'm not entirely sure I'm rebuilding it correctly.

The way this is built, is cleanlink.js is included everywhere and contains all the common code. background.js runs the background "service," inject.js is a content script that is injected in every frame, and options.js is run on the options page.

@Cimbali
Copy link
Author

Cimbali commented Apr 26, 2018

Notifying issues linked to WebExtension: #174, #172, #170

@Cimbali Cimbali force-pushed the master branch 2 times, most recently from f8c9b0d to 9a132ea Compare April 29, 2018 21:11
@alekksander
Copy link

actually why supplement over forking as new project? @diegocr doesn't seem to have any spare time

@Cimbali
Copy link
Author

Cimbali commented Apr 30, 2018

@alekksander We'll see in due time. I'm trying to make this work decently first, then hopefully discuss with @diegocr, and if he indeed doesn't want to maintain the project any more I could take it over.

Cimbali added 12 commits May 1, 2018 04:13
Basically only whitespace and renaming variables and functions. Should
be functionally completely equivalent to previous commit.
- Add a manifest for webext version and remove install.rdf
- Remove files & functions that do setup (bootstrap.js, ...)
- Keep functions that do actual cleaning work (browser.js -> cleanlinks.js)
Remove recursive functions, as content scripts are injected in every frame.
Add messaging between background and content script to send
cleaned-click events and numbers of links cleaned.

In background.js, update the count (on the badget text), notify the
user, and (TODO) track the cleaned links for potential future
whitelisting
Loading options and updating+storing options on change still to be done.
Requires storage permission. Options are stored as they are displayed in
the options page, and loaded based on the object types in the default
values.

All the main code is wrapped in the loadOptions.then() to be sure that
we load options from storage before anything else.

A message is sent to the background script to trigger a new loadOptions()
whenever new values are saved, so that it stays up to date.
@Cimbali
Copy link
Author

Cimbali commented May 1, 2018

Anyone who wants to test the web-extension, please grab web-ext, clone this PR and run web-ext run. Any feedback is welcome.

Alternately, go to about:debugging#addons, tick "Enable add-on debugging", click "Load Temporary Add-on" and select the manifest.json file from this PR. Again, any feedback is welcome.

@jawz101
Copy link

jawz101 commented May 1, 2018

Can you make an xpi of it for those of us not familiar with the extension building?

@Cimbali
Copy link
Author

Cimbali commented May 2, 2018

i'm afraid tere's no xpi's anymore. You don't even have to build anything, just get the source code and follow one of the two ways described above:

Get the source:

git clone https://github.com/diegocr/CleanLinks
cd CleanLinks
git fetch origin pull/176/head:webext
git checkout webext

If you have npm, the node.js package manager:

npm -i web-ext
web-ext run

Alternately, use the debugging mode:

  • Run firefox --new-instance about:debugging#addons
  • tick "Enable add-on debugging",
  • click "Load Temporary Add-on",
  • in the file selection dialog, select chrome.manifest in the directory you just created by cloning (in the "Get the source" step)

That's really all there is to it.

Also repeat the dirty hack to handle links added in a document with
ajax, which consistss of running a clean again after repdelay seconds
after a click.
A new contextMenu item is required for this, as we can't intercept the
previous one anymore in web extensions.

The setup for this feature is a little contrived:
- copying to clipboard can not be done from a background script.
- on links or selections, the target text (link to be cleaned) is
  available from the background script where the contextMenu event is
  handled.
- otherwise, only the script in the page knows where the right click
  that triggered the contextMenu is, thus what the text is.

Thus, organise the flow thusly:
1. In injected content script, on right click, send text link around
   click position to background and wait for (optional) response.
2. In background script, if our contextMenu item is clicked, get sent
   text link and clean it (or use a.href or selected text), and send
   cleaned link as message reply
3. In injected content script, if there is a response to the message,
   copy it to the clipboard.

NB.  Are we leaving hanging promises around? Or do they get GC'd?
NB2. There might be a race condition when accessing lastRightClick, i.e.
     the saved text link message (content script (1.) -> background (2.)).
     If clicked too fast, maybe the message didn't arrive yet. To fix
     that, we probably would need to wrap lastRightClick in another
     promise.
There is a browser preference that controls this:
"When you open a link in a new tab, switch to it immediately"

However this preference is not available through the browserSettings API:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/browserSettings
@alekksander
Copy link

„no xpi's anymore”?
how come? won't this get a release as xpi some day?

@Cimbali
Copy link
Author

Cimbali commented May 6, 2018

WebExtensions are a new API and format for extensions, and the whole reason this addon has to be ported in the first place.

WebExtensions are notshipped as xpi's, just as is or zipped (usually via addons.mozilla.org)

@Lcstyle
Copy link

Lcstyle commented May 7, 2018

well i can at least confirm that this branch does work. I am enjoying it in 59.0.3 x64 win10

@Riajyuu
Copy link

Riajyuu commented May 7, 2018

@Cimbali can you post this on AMO ?

@Cimbali
Copy link
Author

Cimbali commented May 7, 2018

I will publish on AMO as soon as this is at beta-level of readiness. Specifically I want to implement all features before putting this out there, so I still want to do the cleaning of Location: headers and requests initiated by a page such as refreshes.

@alekksander
Copy link

i confirm it's working fine under fx59 on ubuntu based system. could translate to polish if needed, and waiting for the official release to start complaining/suggesting things :)

Cimbali added 11 commits May 13, 2018 16:54
Also move the cleaning function to cleanlinks.js.

Perform the cleaning in the onRequest function by returning the
{redirectUrl: ...} object if we have a cleaned link.

Integrate as comments all fixes from the original XUL code in case those
bugs show up again.
The AMO parser was throwing a syntax error on parsing the previous
version for some reason. Logic remains the same: identify a link in a
jacascript:... text and keep just that.
@Cimbali
Copy link
Author

Cimbali commented May 13, 2018

Alright, the webextension fork is out on AMO! Welcoming bug reports and feature requests at https://github.com/Cimbali/CleanLinks/issues

Get the add-on here: https://addons.mozilla.org/addon/clean-links-webext/

@Cimbali Cimbali closed this May 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants