Skip to content

Commit

Permalink
Merge pull request #7 from perry-mitchell/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
perry-mitchell committed Dec 3, 2023
2 parents 5e2b346 + 3832511 commit f2188fb
Show file tree
Hide file tree
Showing 27 changed files with 3,558 additions and 595 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
@@ -0,0 +1,15 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,package-lock.json}]
indent_size = 2
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,30 @@
name: Tests

on:
push:
pull_request:

jobs:
integration:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v2
- name: Integration tests ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run test:integration
# formatting:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Formatting
# uses: actions/setup-node@v1
# with:
# node-version: 20.x
# - run: npm ci
# - run: npm run test:format
26 changes: 1 addition & 25 deletions .gitignore
@@ -1,27 +1,3 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
/dist
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,3 @@
{
"typescript.preferences.importModuleSpecifierEnding": "js"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,9 @@
# SlyFox Changelog

## v1.0.0
_2023-12-02_

* First major release
* Renamed from `archetype` to `slyfox`
* ESM
* Typescript
36 changes: 0 additions & 36 deletions Gruntfile.js

This file was deleted.

55 changes: 3 additions & 52 deletions README.md
@@ -1,53 +1,4 @@
# archetype
Restore overwritten methods on the window.
# SlyFox
> Restore overwritten methods on the window and document... **Like a fox**.
[![Build Status](https://travis-ci.org/perry-mitchell/archetype.svg)](https://travis-ci.org/perry-mitchell/archetype) [![archetype is on Bower](https://badge.fury.io/bo/archetype.svg)](https://github.com/perry-mitchell/archetype)

## Making repairs
Libraries like Prototype.js do immeasureable harm to the shared environments that are webpages. 3rd party libraries - like those used in tracking and advertising scripts - must ensure that they can cope with whatever broken system is presented to them. Libraries like Prototype assume that what they add is for the benefit of the developer and that single website, and that the extensions they provide won't harm existing functionality - but that's obviously incorrect, and Archetype is here to fix those problems in a simple manner.

### What it does
Archetype provides access to original window methods that may have been overwritten by some script.

### How it does it
The library uses an iframe to create a clean environment in which to extract methods from. When a function is requested from archetype, the main window is checked for a native function - if that function is not native, a native version will be taken from within an iframe, bound to the top-level and returned.

## API
**NB:** archetype is loaded asynchronously, due to the fact that it requires `document.body.appendChild` to be defined for inserting its safe-iframe. Before using archetype, wrap your code in the following:

```javascript
archetype.onReady(function() {

var getElementById = archetype.getNativeMethod("document.getElementById");

});
```

### getNativeMethod(path{, bindContext})
Fetches a native window method at a given path. A path is simply the string representation of a method's location on the window: eg. window.setInterval. `bindContext` is an optional object to bind at the context - if not provided, the window/document will be used instead.

If a method like `window.document.querySelector` is overwritten by some crappy JavaScript, you can retrieve it by calling `var querySelector = archetype.getNativeMethod("window.document.querySelector");`.

In the same way, if you would like to make sure an HTML element has the native function you can call `var qsEl = archetype.getNativeMethod("window.document.querySelector", divElement);`

### isNative(pathOrMethod)
Checks if a method is native (not overwritten). You can pass a path (eg. "window.setTimeout") or an actual function.

### patchMethod(path)
Patches a method on the top window. This function checks to see if the method on the top window is native or not, and if it **isn't**, it is overwritten with a native copy from the _safe_ window from a clean iframe.

## Use-cases

### Fixing overwritten query selectors
Some libraries like Prototype.js and CloudFlare's RocketLoader overwrite methods like `querySelector` and `querySelectorAll`, which is a very stupid idea. Fixing this is simple:

```javascript
["document.querySelector", "document.querySelectorAll"].forEach(archetype.patchMethod);
```

This approach is somewhat fighting fire with fire, so the better option would be to simply keep a reference to a valid selector method:

```javascript
var querySelector = archetype.getNativeMethod("document.querySelector"),
querySelectorAll = archetype.getNativeMethod("document.querySelectorAll");
```
_TBA_.
28 changes: 0 additions & 28 deletions bower.json

This file was deleted.

0 comments on commit f2188fb

Please sign in to comment.