Giter Club home page Giter Club logo

epikloud / chrome-app-user-agent-and-bookmarklet-webview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from riaevangelist/chrome-app-user-agent-and-bookmarklet-webview

0.0 2.0 0.0 344 KB

webviewJS can be used to build communication between a chrome app and a webveiw. It can be called from an app to modify the user agent for analytics if executed at the proper time. Also webviewJS can be used to run bookmarklets in the webview, as well as pass whole js files, or raw, non stringified functions.

License: MIT License

chrome-app-user-agent-and-bookmarklet-webview's Introduction

Chrome App User Agent icon Google Chrome App runWebviewJS script injector for sandboxed and global scopes

Manifest based userAgent example
bookmarklet (ish) example
direct function injection example


example dir

this gives an example of every type of useage as well as implementing the customUserAgentScript in example5 of the js/bookmarklet.js file.

The code will automatically use the data from your manifest file to build its specific user agent. You can further customize the code if you like, but if we all keep it somewhat similar it will help webpage owners see how many users are using chrome apps to view their site. This will help our community as a whole gain traction without confusing website/app developers marketers and owners.


Download and try the demo from the chrome webstore

https://chrome.google.com/webstore/detail/set-user-agent-app-webvie/mpbnpaonopfcidhkmpmfbjflikfafape

example icon author : AhaSoft
example icon license : CC Attribution Non-Commercial No Derivatives

JitterBeast belongs to and was created by The Oatmeal if you don't know who that is, welcome to a new chapter of your previously mindnumbingly boring existance.


how to use


First include the code from runWebviewJS.js in your background (copy & paste) or instance code (as a script tag).


Then listen for the webview to start loading. Either with an annonymous or shared handler function.

Shared (best performance)

function webviewStarted(e){
    ....Do stuff...
}

document.querySelector('webview').addEventListener(
    'loadstart',
    webviewStarted
);

Annonymous (function created on the fly each time)

function webviewStarted(e){
    ....Do stuff...
}

document.querySelector('webview').addEventListener(
    'loadstart',
    function(e){
        ...Do stuff...
    }
);

Next, in your handler function, set the code to send your code or file

runWebviewJS(
    el,             (webview element)
    code,           (code to run OR filename)
    runASAP,        (run code as soon as possible) *false
    requiresGlobal, (needs explicit access to webview window global scope) *false
    isFile,         (load file from app dir) *false
    data            (template object with variables to replace in file) *false
)

Example data object

{
    hello   : 'world',
    numNum  : function(){
        return Math.floor(
            Math.rand()*1000
        );
    }
}

${hello} will be replaced with 'world' and
${numNum} will be replaced with a randomly generated whole number from 0 to 1000 in the specified file. This is only for files. Code snippits should be sent as desired for use or removed into a template file.

Example code ALL CODE MUST BE SENT WRAPPED IN A FUNCTION TO LIMIT SCOPE

function(){
    function bodyClick(e){
        console.log(e);
    }
    
    document.body.addEventListener(
        'click',
        bodyClick
    );
}

OR you can send the same thing as a string. One might want to do this to add in custom constants or variables from the parent scope before sending to the webview.

"function(){function bodyClick(e){console.log(e);}document.body.addEventListener('click',bodyClick);}"

if you want to use a file the code paramater will be the name of the file.

'js/my-awesome-webview-code.js'

Please feel free to leave questions bug requests or any feedback at all.

Special thanks to Fady Samuel and Ken Rockot who helped me point me in the right direction to achive what I was looking for in this.


Written with StackEdit.

chrome-app-user-agent-and-bookmarklet-webview's People

Contributors

riaevangelist avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.